user1475623
user1475623

Reputation:

Pass in a function to a Invoke-Command scriptBlock

I am trying to create a function then call the function when I pass it to the Invoke-Command -scriptBlock. However, when I run this it says that it cannot recognize that function

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}

Upvotes: 0

Views: 2058

Answers (2)

user1475623
user1475623

Reputation:

I figured it out:

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

Upvotes: 0

mjolinor
mjolinor

Reputation: 68273

You create a function to define a script block you can call by name, usually repeatedly. There doesn't seem to be any reason to use a function for what you're doing here, and it's just complicating the process. Just make it a script block, and invoke that:

$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

Upvotes: 4

Related Questions