0

trying to do something insanely simple in PS but for some odd reason it is just not playing ball. My PS script looks like:

For some reason its complaining about the fact that:

enableMSDTC : The term 'enableMSDTC' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Am I not allowed to do this? I have to have a separate script file for each function?

Thanks in advance, DS.

    param
    (
        [string]$folder = $(throw 'Local folder to map to is required.')
    )

    begin
    {
        [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
    }
    process
    {
$hasDrive = Test-Path -Path "D:\"

    if ($hasDrive -eq $true) {
        echo "Enabling MSDTC settings..."
        enableMSDTC    
    }

    Function enableMSDTC() {
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "LuTransactions" -Value "1"
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "NetworkDtcAccess" -Value "1"
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "NetworkDtcAccessAdmin" -Value "1"
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "NetworkDtcAccessClients" -Value "1"
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "NetworkDtcAccessInbound" -Value "1"
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "NetworkDtcAccessOutbound" -Value "1"
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "NetworkDtcAccessTransactions" -Value "1"
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSDTC\Security" -Name "NetworkDtcAccessXaTransactions" -Value "1"        
    }
}

2 Answers 2

1

Define the function before you try to call it.

Sign up to request clarification or add additional context in comments.

2 Comments

Dear god didn't realise it was procedural! Thank you.
Bit me in the butt all the time when I started. :)
0

Powershell is an interpreted language, which means commands are parsed in order (top down) at run time.

You can't call a function or refer to a variable before you define it.

1 Comment

Well, just to be pedantic, Interpreted vs. Compiled language doesn't determine this. For example, VBScript is an interpreted language and you can put functions at the end of a file with no problem at all. It simply depends on the way that the interpreter behaves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.