简体   繁体   English

Powershell脚本调用功能

[英]Powershell script call functions

trying to do something insanely simple in PS but for some odd reason it is just not playing ball. 试图在PS中做一些疯狂的简单操作,但出于某种奇怪的原因,它只是没有发挥作用。 My PS script looks like: 我的PS脚本如下所示:

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. enableMSDTC:术语“ enableMSDTC”未被识别为cmdlet,函数,脚本文件或可运行程序的名称。 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. 预先感谢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"        
    }
}

在尝试调用该函数之前,先对其进行定义。

Powershell is an interpreted language, which means commands are parsed in order (top down) at run time. Powershell是一种解释性语言,这意味着在运行时按顺序(从上到下)解析命令。

You can't call a function or refer to a variable before you define it. 在定义函数之前,不能调用函数或引用变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM