I'm new to powershell and I'm trying to automate creating a DHCP reservation.
So far I'm able to get the IP address like so:
$IP = ( GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter).IpAddresses[0]
This returns a string like:
192.0.2.1
However, the Add-DhcpServer4Resrvation cmdlet does not accept an ip address as a string. It requires the IP address be a 'System.Net.IpAddress'
Add-DhcpServerv4Reservation -ComputerName $DHCPServer -ScopeId $DHCPScope -IPAddress $IP -Client
Id $MacAddress -Name $HVNAME
Add-DhcpServerv4Reservation : Cannot process argument transformation on parameter 'IPAddress'. Cannot convert value "
10.254.130.104
" to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
At line:1 char:86
+ ... ope -IPAddress $IP -ClientId $MacAddress -Name $HVNAME
+ ~~~
+ CategoryInfo : InvalidData: (:) [Add-DhcpServerv4Reservation], ParameterBindingArgumentTransformationEx
ception
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-DhcpServerv4Reservation
How do you convert a string to a System,.Net.IPAddress?
According to this link, it should be easy like
> [ipaddress]"192.0.2.1"
However that doesn't work.
PS C:\Windows\system32> $FOO = [IPAddress]$IP
Cannot convert value "
10.254.130.104
" to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
At line:1 char:1
+ $FOO = [IPAddress]$IP
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastParseTargetInvocation
tworkAdapter) Format-List -Property *.254.13༁爼ሂÌGEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter) | Format-List -Property * {༁牎ᐂÊGEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter | Format-List -Property *
ఁ牘ࠂÆ$IP = ( GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Gt-VMNex뿰bpte
Related question
Powershell, get ip4v address of VM
[IPAddress] Wont work if there are spaces in the string
Remove them with Trim()
$IP = [IPAddress]$IP.Trim()
A completely different approach for obtaining IP Address of a server is:
by passing the Workstation/Server name as a parameter to the input type System.Net.DNS cast
For example, if the FQDN name of the host is ABCTest-DEV, then the following script will reveal the IP address of ABCTest-Dev provided, the host has a DNS record already available in the domain.
$ipaddr = [System.Net.Dns]::GetHostAddresses('ABCTest-Dev')|Where AddressFamily -EQ 'InterNetwork'
Write-Host 'This IPV4 Address of the Host is: '$ipaddr
Related
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.
I am relatively new to PS, but using PS within a wider IaC workflow. I have the following script, which checks for number of services installed, and increments the port number variable by 1.
$Service = Get-Service Test* | Select-Object Name
If( $Service.Name.count -eq 0){
$port = 12000 }
If( $Service.Name.count -eq 1){
$port = 12001 }
If ( $Service.Name.Count -eq 2){
$port = 12002 }
If ( $Service.Name.Count -eq 3){
$port = 12003 }
Unfortunately, this is not as dynamic as I would like, as the script will fail if there is more than 3 services.
How can I dynamically increment the port number based on number of services that exist? The port number starts at 12000, then if another service is installed, the port will be 12001, and if a third service is detected, the port number is 12002 and so on.
You can follow the advice in comment by AdminOfThings if you ensure that the $Service object is a collection:
$Service = #(Get-Service Test* | Select-Object Name)
$port = 12000 + $Service.Count
Otherwise, you encounter the following errors:
$Service = Get-Service Test* ; $Service; $Service.Count
The property 'Count' cannot be found on this object. Verify that the property exists.
At line:1 char:42
+ $Service = Get-Service Test* ; $Service; $Service.Count
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
$Service = Get-Service Te* ; $Service; $Service.Count
Status Name DisplayName
------ ---- -----------
Running TermService Remote Desktop Services
The property 'Count' cannot be found on this object. Verify that the property exists.
At line:1 char:40
+ $Service = Get-Service Te* ; $Service; $Service.Count
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
I`m following the documentation:
https://googlecloudplatform.github.io/google-cloud-powershell/#/google-compute-engine/GceInstance/Set-GceInstance
I cant get the following code working:
$disk = Get-GceDisk disk-snapshot-instance-1
Set-GceInstance -Name instance-1 -AttachDisk $disk
When I replace $disk for disk-snapshot-instance-1 I get the same error:
Set-GceInstance : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Set-GceInstance -Name instance-1 -AttachDisk $disk
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-GceInstance], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Google.PowerShell.ComputeEngine.SetGceInstanceCmdlet
The thing I don't understand is that it works for me to remove a disk when attaching it manually through the G-cloud interface.
Set-GceInstance -Name instance-1 -RemoveDisk $disk
My question:
Why can't I attach a disk to an instance with the above code, while removing the disk works?
The correct commands are:
Set-GceInstance $instance -AddDisk $disk1 -Zone $zone
Set-GceInstance $instance -RemoveDisk $disk1 -Zone $zone
I create an instance with 2 disks using gcloud and then:
Get-GceDisk | Format-List -Property Name
This returns:
Name : disk-1
Name : disk-2
Name : instance-1
Then I can:
$zone = "us-west1-c"
$disk2 = Get-GceDisk disk-2
Set-GceInstance instance-1 -RemoveDisk $disk2 -Zone $zone
Set-GceInstance instance-1 -AddDisk $disk2 -Zone $zone
HTH
I am using the below Code to get the content and test it using TCP-netconnection in Powershell. But, I am getting an Error like below.
Test-NetConnection : Cannot process argument transformation on parameter 'ComputerName'. Cannot convert value to type System.String. At line:2 char:34
+ Test-NetConnection -ComputerName $Name -Port 445
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Test-NetConnection], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Test-NetConnection
Below is script command that I have used.
$Name = Get-Content "C:\Users\vishnuvardhan.chapal\Documents\Test File.txt"
Test-NetConnection -ComputerName $Name -Port 445
Does anyone have an idea on, "How to Convert read data into a string"?
Your $name variable potentially has an array of computer names stored in it. The computername parameter only accepts one string. You will need to loop through your computer names like the following:
$Name = Get-Content "C:\Users\vishnuvardhan.chapal\Documents\Test File.txt"
$name | foreach-object {test-netconnection -computername $_ -port 445}
Your test file.txt file needs to have only computer names in it. They need to be one computer name per line format.
Back when I had Windows 7 an a lower version of Powershell the following code use to work without any issues.
It checks each server in a text file for some services and dumps the results to a CSV.
Now that I'm on Windows 10 and with Powershell v5 I get this error message:
Get-Service : Cannot open Service Control Manager on computer 'tfsserver1'. This operation might require other privileges. At
C:\Users\Razon\Desktop\Patching\ServerServices_Checker_v2.ps1:48
char:4
+ (Get-Service -Name TFSJobAgent*,IIS*,World* -ComputerName $_) | Select Machine ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Service], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand
####System Varialbe to User's Deskotp
$filePath = [Environment]::GetFolderPath("Desktop")
Here is the code:
function tfsCheck
{
$Path = "$filePath\Patching\Servers\tfs_servers.txt"
Get-Content $Path | foreach {
(Get-Service -Name TFSJobAgent*,IIS*,World* -ComputerName $_) | Select MachineName, Status, DisplayName
}
}
#TFS Function Call and Write to CSV
tfsCheck|Select MachineName, Status, DisplayName |Export-Csv $filePath\Patching\Results\TFS_ServicesResults.csv -NoTypeInformation
To resolve this issue, elevate the user's network privileges to be able to access the Service Control Manager on the Server.
https://support.microsoft.com/en-in/help/964206/cannot-open-service-control-manager-on-computer-servername-.-this-operation-might-require-other-privileges