【问题标题】:Pass in a function to a Invoke-Command scriptBlock将函数传递给 Invoke-Command scriptBlock
【发布时间】:2014-05-09 02:58:13
【问题描述】:

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

【问题讨论】:

  • 为了让它更像 PowerShell,您可以将所有 reg add 命令替换为 Set-Itemproperty

标签: powershell


【解决方案1】:

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

$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

【讨论】:

  • 好的。帮助我了解使它们起作用而不是简单地分配给变量的脚本块有什么优势。任何一个都可以在调用命令中调用。
  • 函数提供程序支持使用脚本块作为本地会话的函数。无论是存储在函数中还是变量中,脚本块都是相同的。如果它是作为函数创建的,但从未在本地会话中作为函数调用,那么您只是在滥用函数提供程序,将其作为远程脚本块的垃圾场。
  • 您正在创建一个本地函数,然后必须从中撬出脚本块以发送到远程服务器以运行。我不明白这有多“简单”。
  • 事实上,不久前我想到了I answered essentially the same question with essentially the same solution。唯一的区别是我将脚本的内容分配给了一个字符串,然后将其插入到花括号中。这种方式更好,因为它节省了四次击键并使格式更整洁。在我得知可以直接将脚本块分配给变量之前,我一定已经写了这个答案。
  • 更进一步,我明白他在说什么。他没有像你一样理解我的问题!问题又是这样的:“如何在脚本块中调用函数?”我想这很难理解?
【解决方案2】:

我想通了:

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

【讨论】:

  • 这与您已经得到的答案基本相同... function: item 的内容是一个脚本块,用于定义它。您刚刚使用变量/提供程序语法来访问此内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
相关资源
最近更新 更多