将函数传递给Invoke-Command scriptBlock

时间:2014-05-08 18:49:44

标签: powershell

我正在尝试创建一个函数,然后在将它传递给Invoke-Command -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}

2 个答案:

答案 0 :(得分:4)

您可以创建一个函数来定义可以按名称调用的脚本块,通常是重复调用。似乎没有任何理由使用函数来处理你在这里所做的事情,而这只会让这个过程变得复杂。只需将其设为脚本块,然后调用:

$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

答案 1 :(得分:0)

我明白了:

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