I am trying to pass value for argument in below command script called install.ps1. I execute it by ./install.ps1 HD1
invoke-command -Session $session -ScriptBlock {G:\usr\sap\$($args[0])\hdbclient\hdbuserstore.exe list}
but it gave an error to me that
The term 'G:\usr\sap\$($args[0])\hdbclient\hdbuserstore.exe' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (G:\usr\sap\$($a...dbuserstore.exe:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : hostname
You are not using the -Argumentlist parameter on Invoke-Command. Let me show an example below. Use the Param method inside the scriptblock if you want to use custom variable names.
$Directory = "HD1"
$Scriptblock = {
param($Var1)
G:\usr\sap\$Var1\hdbclient\hdbuserstore.exe list
}
invoke-command -Session $session -ScriptBlock $Scriptblock -ArgumentList $Directory
Related
I'm creating a function to use in my Powershell Profile that will allow me to change users. Below is what I have:
function switch-user {
Param(
[Parameter(Mandatory=$true)]
[ValidateSet("adminUser","normalUser")]
$User="normalUser"
)
$Account1 = "$env:USERDOMAIN\normalUser"
$Account2 = "$env:USERDOMAIN\adminUser"
$AccountPassword = Read-Host -AsSecureString
switch($User) {
'normalUser' {$username = $Account1 ; $pw = $AccountPassword}
'adminUser' {$username = $Account2 ; $pw = $AccountPassword}
}
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$password
New-PSSession -Credential $cred | Enter-PSSession
}
When running the function (i.e. switch-user adminUser), I receive the following error:
Param : The term 'Param' is not recognized as the name of a cmdlet, function, script file, or
operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At C:\Users\normalUser\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:7 char:5
+ Param(
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (Param:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I'm fairly new to Powershell. Am I incorrectly declaring the Param? I know it needs to be the first line in a function, so what am I missing?
hi i' m trying to retrieve the vhdx of specified vmname from the remote host server
here part of the script
$pth="C:\path\resize-vm"
$list=gc $pth\list-host.txt
foreach ($hostserver in $list) {
$vm=(Invoke-Command -ComputerName $hostserver -ScriptBlock {Get-VM}).VMName
Write-Host -NoNewline " here the vm installed in " $hostserver `r`n $vm
$vmname=Read-Host -Prompt "please chose a vmname to resize "
#the issue in the last line
$pathvhd=Invoke-Command -ComputerName $hostserver -ScriptBlock {(Get-VMHardDiskDrive -VMName $vmname).path}
When I launch this command $vmname="dc-kozhan"
I am getting this error
Cannot validate argument on parameter 'VMName'. The argument is null
or empty. Provide an argument that is not null or empty, and then try
the command again.
+ CategoryInfo : InvalidData: (:) [Get-VMHardDiskDrive], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.HyperV.PowerShell.Commands.GetVMHardDiskDrive
Command
+ PSComputerName : h-uludag-3
But when I specified dc-kozhan literally it work
PS C:\Users> Invoke-Command -ComputerName $hostserver -ScriptBlock {(Get-VMHardDiskDrive -VMName "DC-KOZAHAN")
.path}
V:\DC-KOZAHAN\DC-KOZAHAN-SYSTEM.vhdx
V:\DC-KOZAHAN\DC-KOZAHAN-DIRECTORY.vhdx
V:\DC-KOZAHAN\DC-KOZAHAN-SYSVOL.vhdx
V:\DC-KOZAHAN\DC-KOZAHAN-BACKUP.vhdx
does anyone have an idea why it does not work when it's specified in a variable
You need to understand how to pass arguments inside the scriptblock. The scope is different when you try to pass the value inside the scriptblock. As a result, $VMname is becoming null in your first statement.
Kindly change your existing Invoke-command statement to the below one:
Invoke-Command -ComputerName $hostserver -ScriptBlock {Param([string]$vmname)(Get-VMHardDiskDrive -VMName $vmname)} -ArgumentList $vmname
Also, my suggestion for you to read about the argumentlist in powershell in case invoke-command
Hope it helps.
$buildDef = "Service.xxxx"
$buildDefFull="MyProject/$buildDef"
Start-Job -Name 'Service1' -ScriptBlock { tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection" /builddefinition:"$buildDefFull" }
i get this error:
Option builddefinition requires a value.
+ CategoryInfo : NotSpecified: (Option builddefinition requires a value.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : localhost
i can't seem to get tfsbuild to accept the parameter in a start job... it actually runs fine if i just do the tfsbuild part with no job.
Any ideas how i'm supposed to pass that value?
tx
The $buildDefFull variable is outside the scope of the scriptblock.
You have 2 options:
PowerShell 3+
Use the Using scope modifier:
$buildDef = "Service.xxxx"
$buildDefFull="MyProject/$buildDef"
Start-Job -Name 'Service1' -ScriptBlock { tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection" /builddefinition:"$Using:buildDefFull" }
Any Version
Define and pass parameters to the scriptblock:
$buildDef = "Service.xxxx"
$buildDefFull="MyProject/$buildDef"
Start-Job -Name 'Service1' -ScriptBlock { param($bdf) tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection" /builddefinition:"$bdf" } -ArgumentList $buildDefFull
I am trying to run an executable with a config file as a parameter using invoke-command through a PowerShell script.
Here is what I have so far in my PowerShell script:
$config = 'D:\EmailLoader\App.config'
invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config) & 'D:\EmailLoader\GetMailAndAttachment.exe' $config} -ArgumentList $config
I then Execute the PowerShell script with the following command:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -File "D:\PowerShellScripts\Email.ps1"
And I get the following error:
Unexpected token 'config' in expression or statement.
At D:\PowerShellScripts\Email.ps1:3 char:121
+ invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config) 'D:\EmailLoader\GetMailAndAttachment.exe' $config <<<< } -ArgumentList $config
+ CategoryInfo : ParserError: (config:String) [], ParentContainsE rrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
The code looks like it should work. But the exception message is clearly missing the & symbol. I would check that first, because you are getting the exact same message I would anticipate to get when the & was missing. So maybe there is problem with the saved file, rather than with your code.
Side note: If you are on PowerShell 3.0 or newer you should consider using the $using: scope in the script block to avoid adding the param and -ArgumentList.
$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {
&'D:\EmailLoader\GetMailAndAttachment.exe' $using:config
}
There is no need to pass $config as a parameter to a ScriptBlock. Also not needed to add the $config parameter twice. This should work:
$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {&('D:\EmailLoader\GetMailAndAttachment.exe')} -ArgumentList #($config)
I want to start a background job and capture it's process id into a .pid file. I was able to do it with the Start-Process as follows:
Start-Process C:\process.bat -passthru | foreach { $_.Id } > start.pid
Now, I want to wrap Start-Process with Start-Job, to run it in the background, like this:
$command = "Start-Process C:\process.bat -passthru | foreach { $_.Id }"
$scriptblock = [Scriptblock]::Create($command)
Start-Job -ScriptBlock $scriptblock
Unfortunatelly, this doesn't work and Receive-Job gives me the following error:
The term '.Id' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (.Id:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : localhost
Looks like it's something wrong with the $_ variable. Maybe it gets overwritten by the Start-Job.
Any clues greatly welcome!
That is because the variable is being expanded when using double quotes. If you want to keep the $_, then you need to use single quotes.
$command = 'Start-Process C:\process.bat -passthru | foreach { $_.Id }'
$scriptblock = [Scriptblock]::Create($command)
Start-Job -ScriptBlock $scriptblock