I'm trying to write a one-liner to leverage some of the capabilities of netbackup remotely. I know how to pass parameters to Invoke Command using -args[0] and [1] at the end, with repeating parameters. An example of what I'm trying to accomplish:
CC = Country Code (Will repeat due to the naming conventions
SS = Site (Also repeats due to naming convention)
Invoke-Command -ComputerName RemoteServer -ScriptBlock {& "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe" CC0SITE_VMW_BRON -set -L -M CC0SITEb0100d0a.s0SITE.CC.DOMAIN.COM}
After getting user-input and declaring the parameters, it doesn't seem to pass to the invoke-command
Invoke-Command -ComputerName RemoteServer -ScriptBlock {& "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe" $args[0]0$args[1]_VMW_BRON -L -M $args[0]0$args[1]b0100d0a.s0$args[1].$args[0].DOMAIN.com} -Args $CCode, $Site
Use param($val1,...) inside the scriptblock to pass the arguments.
Invoke-Command -ComputerName 'SERVERNAME' -ScriptBlock {
param($argument1, $argument2) #<--- this is required!
write-host $CCode
write-host $Site
} -ArgumentList ($argument1, $argument2)
More information and syntax can be found at ArgumentList (alias Args) section for Invoke-Command cmdlet.
You may have an issue with the way you're expanding your variables and hence it may appear the args are not being passed correctly, when I'm debugging I simply use write to test the output. For example:
Invoke-Command -ComputerName localhost -ScriptBlock {write "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe CC0SITE_VMW_BRON -set -L -M CC0SITEb0100d0a.s0SITE.CC.DOMAIN.com"}
Invoke-Command -ComputerName localhost -ScriptBlock {write "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe $($args[0])0$($args[1])_VMW_BRON -set -L -M $($args[0])0$($args[1])b0100d0a.s0$($args[1]).$($args[0]).DOMAIN.com"} -Args "CC", "SITE"
Related
A module I am importing has several Write-Host commands that I wish to suppress (redirect to NULL).
This works great when I run it directly on a machine:
Import-Module 'path\to\module\module.ps1' 2>&1 > $null
When running the exact same on a remote host using Invoke-Command, the redirect is ignored and all the output from the module shows up:
Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
Import-Module 'path\to\module\module.ps1' 2>&1 > $null
}
I've tried different ways of re-direct (i.e. Out-Null, etc), all with the same result.
You could use 6>> $null .
so:
Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
Import-Module 'path\to\module\module.ps1' 6>> $null
}
This is the "Example 5: Suppress output from Write-Host" provided by Microsoft, modified accordingly to "about_Operators".
I have the following script block:
$scriptBlock = {Start-Process ping.exe -ArgumentList localhost -Wait -NoNewWindow -PassThru}
Note: I am using a process as I want to be able to set the working directory the exe is executed in.
If I invoke it locally like this:
Invoke-Command -ScriptBlock $scriptBlock
I get the full ping output displayed. But when I invoke it remotely like this:
Invoke-Command -ComputerName RemoteComputerName -ScriptBlock $scriptBlock
I don't see any of the ping output. How can I get the remote output to display locally?
Drop the Start-Process and invoke the command directly:
$scriptBlock = {ping.exe localhost}
or via the call operator:
$scriptBlock = {& ping.exe localhost}
If you need to run the command from a particular directory, simply change to that directory before running the command:
$scriptBlock = {
Set-Location 'C:\some\folder'
& ping.exe localhost
}
I'm tearing my hair out trying to invoke-command but pass the path to the exe as a parameter
eg:
I want to take this command
powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($command ) C:\windows\system32\getmac.exe /$command } -ArgumentList ?
and translate it into a form like this
powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) $path\getmac.exe /$command } -ArgumentList C:\windows\system32,?
I've tried all manner of quoting, ampersands and other contortions but can't get it to work. The above attempt results in
Unexpected token '\getmac.exe' in expression or statement.
At line:1 char:97
(I don't really want to invoke getmac on localhost, this is the runnable, SO distilled version)
Try this option. It shows me help for cscript.exe.
C:\>powershell.exe Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) cmd /c $path $command } -args '"C:\windows\system32\cscript.exe"','"/?"'
I tried other options using & and then path and arguments and it was giving me missing } exception. Then using cmd /c instead of & inside scriptblock fixed the issue.
Powershell won't parse a string as a command that way. For e.g. if you do this:
$path="C:\Windows\System32"
$path\getmac.exe
You would get the same error. The trick to work around this is to use the invoke operator &:
&$path\getmac.exe
or in your example, like this (also note that for a command that you pass to the powershell executable, you must wrap it in scriptblock braces):
powershell -command {Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) &$path\getmac.exe /$command } -ArgumentList C:\windows\system32,?}
All I am trying to do is to execute a batch file in remote machines as a job.I have batch file located in all machines inside C:\Users\temp folder.Here is my code
$batFile = "test.bat"
foreach($m in $machine)
{
Invoke-Command -ComputerName $m -ScriptBlock{param($batFile) & cmd.exe /c "C:\Users\temp\$batFile"} -Authentication negotiate -Credential $cred -ArgumentList $batFile -AsJob
}
But I keep getting
The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script
block, or a CommandInfo object
I tried using $using:batFile inside ScriptBlock as well with no success. Can anyone suggest me what I might be doing wrong? I am using powershell version 4.
do a trace on that,
Invoke-Command -ComputerName $m -ScriptBlock{param($batFile) Trace-Command NativeCommandParameterBinder -Expression { & cmd.exe /c "C:\Users\temp\$batFile"}} -Authentication negotiate -Credential $cred -ArgumentList $batFile -AsJob
and Try using Invoke-Expression as a workaround instead of &
invoke-Expression "cmd.exe /c 'C:\Users\temp\$batFile'"
Regards,
Kvprasoon
I have an advanced function Copy-FilesHC that is available in a module file. This function copies some files from the Source to the Destination folder and generates some output for in a log file.
The function works fine locally:
Copy-FilesHC -Source $Src -Destination $Des *>> $Log
It also works on a remote machine:
# For remote use we need to make it available first
Import-Module (Get-Command Copy-FilesHC).ModuleName
Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList $LocalSrc, $LocalDes
However, I can't seem to figure out how I can have it pass the output to a log file like in the first command. When I try the following, it fails:
Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC *>> $Log} -ArgumentList $LocalSrc, $LocalDes
Invoke-Command : Cannot validate argument on parameter 'ScriptBlock'. The argument is null. Provide a vali
d value for the argument, and then try running the command again.
As indicated here I thought the $ sign for the ScriptBlock was incorrect. But this way I don't need to put my advanced function in a ScriptBlock to copy it over as it now happens automatically while it's only available within the module. So I just need to find out how to capture the output in the log file.
Thank you for your help.
Found the solution just a few minutes ago:
# For remote use we need to make it available first
Import-Module (Get-Command Copy-FilesHC).ModuleName
Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList $LocalSrc, $LocalDes *>> $Log