Powershell Add-DhcpServerv4Reservation Error [duplicate] - powershell

This question already has answers here:
Why does the `using` scope work locally with Start-Job, but not Invoke-Command?
(3 answers)
How do I pass a local variable to a remote `Invoke-Command`? [duplicate]
(2 answers)
Closed 12 months ago.
I'm getting IP address of vShpere VM by using this command:
$VMIPAddress = (Get-VM -Name $VMName).Guest.IPAddress | Select-Object -First 1
and I'm trying to add DHCP reservation by using this command:
Invoke-Command -ComputerName mdc1.ad.morphisec.com -ScriptBlock{
Add-DhcpServerv4Reservation -ScopeId 192.168.0.0 -IPAddress $VMIPAddress -ClientId $VMMacAddress -Description $VMname
}
But I'm getting this error all the time:
Cannot validate argument on parameter 'IPAddress'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
+ CategoryInfo : InvalidData: (:) [Add-DhcpServerv4Reservation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Add-DhcpServerv4Reservation
+ PSComputerName : mdc1.ad.morphisec.com
I understand this is happening because the variable $VMIPAddress is string...
$VMIPAddress.GetType().name
String
But how can I convert it to Integer/IPaddress and pass it to the Add-DhcpServerv4Reservation command?

To pass variables to a session in another computer with Invoke-Command you need to use the using prefix to the variable name. Such as:
Invoke-Command -ComputerName mdc1.ad.morphisec.com -ScriptBlock{
Add-DhcpServerv4Reservation -ScopeId 192.168.0.0 -IPAddress $using:VMIPAddress -ClientId $using:VMMacAddress -Description $using:VMname
}
As for a reference read the about_Remote_Variables conceptual help section.

It's a variable scope issue:
$VMIPAddress = (Get-VM -Name $VMName).Guest.IPAddress | Select-Object -First 1
is creating a local variable, so it's not recognized by the -Scriptblock parameter for Invoke-Command, because it has its own scope. You need to send it to the -ArgumentList parameter.

Related

cannot validate argument on parameter "Path" The argument is null or empty

I was trying to create a simple script that would go to a server and get the acl details of a folder. I tested the command:
Invoke-command -Computername Servername -ScriptBlock {(Get-Acl "\\Server\Folder\user folders").access | ft- auto}
This worked ok. However when I was trying to put it into a script that would allow me to enter the path in via a variable I always get:
Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply an argument that is not null or empty and then
try the command again.
+ CategoryInfo : InvalidData: (:) [Get-Acl], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAclCommand
Here is my script:
#get folder permissions from remote computer
$serverName = read-host "Please enter the name of the target server"
$folderPath = "\\server_name\Folder\user folders"
#read-host "Please enter the full path to the target folder"
Invoke-command -ComputerName $serverName -ScriptBlock {(get-acl $folderPath).access | ft -wrap}
Its probably something very simple, but I'd appreciate the help.
The issue is because you are trying to use the $folderPath variable, but on the remote computer that variable does not exist.
You will need to pass it through as an argument. There are multiple ways to do so, two such ways are below:
# Add desired variable to ArgumentList and define it as a parameter
Invoke-command -ComputerName $serverName -ArgumentList $folderPath -ScriptBlock {
param($folderPath)
(get-acl $folderPath).access | ft -wrap
}
OR
# In PS ver >= 3.0 we can use 'using'
Invoke-command -ComputerName $serverName $folderPath -ScriptBlock {(get-acl $using:folderPath).access | ft -wrap}

retrieve vhdx path from a remote server

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.

How to use variable as an argument for command in "Invoke-Expression" [duplicate]

This question already has answers here:
How do I pass a local variable to a remote `Invoke-Command`? [duplicate]
(2 answers)
Closed 3 years ago.
I'm getting the error
Cannot bind argument to parameter 'Command' because it is null.
+ CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
+ PSComputerName : X
when I try to use
$command = "cmd.exe /c $($currentLocation)/Ping.cmd"
Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {Invoke-Expression -Command: $command }
How do I use a variable like this? I have this as a requirement so I can't use it directly, it needs to be dynamic.
You can either use the -ArgumentList parameter with Invoke-Command or access your variable using $using: like this:
Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {
Invoke-Expression -Command: $using:command
}

Property is empty when run through Invoke-Command

I am trying to individually monitor memory usage of a process (w3wp.exe) that has multiple instances of itself by filtering out a string found in the process' CommandLine property.
It works when I run this script locally:
$proc = (WmiObject Win32_Process -Filter "Name = 'w3wp.exe'" | Where-Object {$_.CommandLine -like "*SomeTextFromCl*"})
$id = $proc.ProcessId
$ws = [math]::round((Get-Process -Id $id).WS/1MB)
Write-Host $ws
However, when I try to run it remotely through Invoke-Command, I get an error telling that the Id property's value is null:
Cannot bind argument to parameter 'Id' because it is null.
+ CategoryInfo : InvalidData: (:) [Get-Process], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetProcessCommand
+ PSComputerName : RemoteServerName
My Invoke-Command syntax is:
Invoke-Command -ComputerName RemoteServerName -FilePath script.ps1 -Credential $mycredential
I'm sure it's simple but I'm back to PS after a long absence and I had a look around but couldn't find anything really helpful.
You are writing the answer to the console. You use the ps1 as a function, so you should use:
return $ws
instead of
write-host $ws

PowerShell Positional parameter cannot be found that accepts argument 'Get-WindowsFeature'

Calling out for some PowerShell help again !!
I have the below which is trying to connect to a remote machine and return all the installed WindowsFeatures.. then I will try to export the list (but I have not been able to that far yet).
Invoke-Command -ComputerName VM01 -ScriptBlock { Get-Module ServerManager Get-WindowsFeature | Where-Object{$_.Installed -match "True" } | Select-Object -Property Name } -credential TestUser01
After running the above I am correctly presented with a login screen to allow me to enter a password then the following error appears, I don't believe I know enough about PowerShell and the use of Positional parameters;
A positional parameter cannot be found that accepts argument 'Get-WindowsFeature'.
+ CategoryInfo : InvalidArgument: (:) [Get-Module], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetModuleCommand
Any pointing in the right direction will be a great help !! thanks guys and girls.
You miss a ; after ServerManageralso, you probably mean Import-Moduleinstead of Get-Module:
Invoke-Command -ComputerName VM01 -ScriptBlock { Import-Module ServerManager; Get-WindowsFeature | Where-Object{$_.Installed -match "True" } | Select-Object -Property Name } -credential TestUser01