Powershell 将函数传递给调用命令脚本块

Powershell 将函数传递给调用命令脚本块,powershell,Powershell,我试图创建一个函数,然后在将其传递给Invoke命令-scriptBlock时调用该函数。但是,当我运行它时,它会说它无法识别该函数 Function Feature { # Install Features Add-WindowsFeature as-dist-transaction,as-was-support , fs-fileserver, web-server,web-webserver,web-app-dev,web-dav-publishing,web-http-re

我试图创建一个函数,然后在将其传递给Invoke命令-scriptBlock时调用该函数。但是,当我运行它时,它会说它无法识别该函数

Function Feature 
{
   # Install Features
   Add-WindowsFeature as-dist-transaction,as-was-support , fs-fileserver, web-server,web-webserver,web-app-dev,web-dav-publishing,web-http-redirect,web-http-tracing,web-lgcy-mgmt-console,web-metabase, web-mgmt-compat,web-mgmt-service,web-scripting-tools,web-dyn-compression,web-windows-auth,net-http-activation,net-non-http-activ, rsat-web-server, telnet-client, SNMP-Service
   # Agent Contact
   reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysContact /t REG_SZ /d "Production Team" /f
   # Agent Location
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysLocation /t REG_SZ /d "Pleasant Hill" /f
# Agent Services (all)
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysServices /t REG_DWORD /d 79 /f
# Community Name
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\!snmp%AM!" /v 1 /t REG_SZ /d "amprdsys04.assetmark.com" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v "!snmp%AM!" /t REG_DWORD /d 4 /f
# Restart the SNMP Service
restart-service snmp
# Configure the Windows Management Instrumentation service recovery time
sc.exe failure Winmgmt reset= 86400 actions= restart/60000/restart/60000/none/60000
# Install Other Required Features
D:\Install\ASPNetMVC3\AspNetMVC3Setup.exe /q
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe /i
# end
}

Invoke-Command -ComputerName Computer123-ScriptBlock {Feature}

您可以创建一个函数来定义一个脚本块,该脚本块可以按名称调用,通常是重复调用。似乎没有任何理由在这里使用函数,这只会使过程复杂化。只需将其设置为脚本块,然后调用:

$InstallFeatures =
{
   # Install Features
   Add-WindowsFeature as-dist-transaction,as-was-support , fs-fileserver, web-server,web-webserver,web-app-dev,web-dav-publishing,web-http-redirect,web-http-tracing,web-lgcy-mgmt-console,web-metabase, web-mgmt-compat,web-mgmt-service,web-scripting-tools,web-dyn-compression,web-windows-auth,net-http-activation,net-non-http-activ, rsat-web-server, telnet-client, SNMP-Service
   # Agent Contact
   reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysContact /t REG_SZ /d "Production Team" /f
   # Agent Location
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysLocation /t REG_SZ /d "Pleasant Hill" /f
# Agent Services (all)
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysServices /t REG_DWORD /d 79 /f
# Community Name
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\!snmp%AM!" /v 1 /t REG_SZ /d "amprdsys04.assetmark.com" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v "!snmp%AM!" /t REG_DWORD /d 4 /f
# Restart the SNMP Service
restart-service snmp
# Configure the Windows Management Instrumentation service recovery time
sc.exe failure Winmgmt reset= 86400 actions= restart/60000/restart/60000/none/60000
# Install Other Required Features
D:\Install\ASPNetMVC3\AspNetMVC3Setup.exe /q
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe /i
# end
}

Invoke-Command -ComputerName Computer123 -ScriptBlock $InstallFeatures

您可以创建一个函数来定义一个脚本块,该脚本块可以按名称调用,通常是重复调用。似乎没有任何理由在这里使用函数,这只会使过程复杂化。只需将其设置为脚本块,然后调用:

$InstallFeatures =
{
   # Install Features
   Add-WindowsFeature as-dist-transaction,as-was-support , fs-fileserver, web-server,web-webserver,web-app-dev,web-dav-publishing,web-http-redirect,web-http-tracing,web-lgcy-mgmt-console,web-metabase, web-mgmt-compat,web-mgmt-service,web-scripting-tools,web-dyn-compression,web-windows-auth,net-http-activation,net-non-http-activ, rsat-web-server, telnet-client, SNMP-Service
   # Agent Contact
   reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysContact /t REG_SZ /d "Production Team" /f
   # Agent Location
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysLocation /t REG_SZ /d "Pleasant Hill" /f
# Agent Services (all)
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\RFC1156Agent" /v sysServices /t REG_DWORD /d 79 /f
# Community Name
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\!snmp%AM!" /v 1 /t REG_SZ /d "amprdsys04.assetmark.com" /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v "!snmp%AM!" /t REG_DWORD /d 4 /f
# Restart the SNMP Service
restart-service snmp
# Configure the Windows Management Instrumentation service recovery time
sc.exe failure Winmgmt reset= 86400 actions= restart/60000/restart/60000/none/60000
# Install Other Required Features
D:\Install\ASPNetMVC3\AspNetMVC3Setup.exe /q
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe /i
# end
}

Invoke-Command -ComputerName Computer123 -ScriptBlock $InstallFeatures
我想出来了:

Invoke-Command -ComputerName Computer123-ScriptBlock ${Function:Feature}
我想出来了:

Invoke-Command -ComputerName Computer123-ScriptBlock ${Function:Feature}

好啊请帮助我了解使它们发挥作用而不是简单地作为分配给变量的脚本块有什么好处。可以在invoke命令中调用其中任何一个。函数提供程序支持将脚本块用作本地会话的函数。脚本块无论存储在函数还是变量中都是相同的。如果它是作为函数创建的,但从未在本地会话中作为函数调用,那么您只是滥用了函数提供程序,将其作为远程脚本块的转储地。您创建的是本地函数,然后必须从中撬出脚本块以发送到远程服务器运行。我不认为这是多么的“简单”。事实上,我不久前就想到了这一点。唯一的区别是,我将脚本的内容指定给一个字符串,然后将其插入大括号中。这种方法更好,因为它节省了四次击键并使格式更整洁。在我知道可以直接将scriptblock分配给变量之前,我一定已经写下了这个答案。而且,我理解了他的意思。他和你一样不理解我的问题!问题是:“如何在脚本块中调用函数?”我想这很难理解?好吧。请帮助我了解使它们发挥作用而不是简单地作为分配给变量的脚本块有什么好处。可以在invoke命令中调用其中任何一个。函数提供程序支持将脚本块用作本地会话的函数。脚本块无论存储在函数还是变量中都是相同的。如果它是作为函数创建的,但从未在本地会话中作为函数调用,那么您只是滥用了函数提供程序,将其作为远程脚本块的转储地。您创建的是本地函数,然后必须从中撬出脚本块以发送到远程服务器运行。我不认为这是多么的“简单”。事实上,我不久前就想到了这一点。唯一的区别是,我将脚本的内容指定给一个字符串,然后将其插入大括号中。这种方法更好,因为它节省了四次击键并使格式更整洁。在我知道可以直接将scriptblock分配给变量之前,我一定已经写下了这个答案。而且,我理解了他的意思。他和你一样不理解我的问题!同样的问题是:“如何在脚本块中调用函数?”我想这很难理解?这与你已经得到的答案基本相同。。。
函数的内容:
项是一个脚本块,用于定义它。您只是使用变量/提供程序语法来访问此内容。这与您已经得到的答案基本相同。。。
函数的内容:
项是一个脚本块,用于定义它。您刚刚使用了变量/提供程序语法来访问此内容。若要使其更像PowerShell,可以将所有
reg add
命令替换为
Set Itemproperty
。若要使其更像PowerShell,可以将所有
reg add
命令替换为
Set Itemproperty