Skip to content

Instantly share code, notes, and snippets.

@mikebranstein
Created August 24, 2016 17:52
Show Gist options
  • Save mikebranstein/d5fabe101bcc705edfbdaa4f4bb3fc36 to your computer and use it in GitHub Desktop.
Save mikebranstein/d5fabe101bcc705edfbdaa4f4bb3fc36 to your computer and use it in GitHub Desktop.
Full code for renaming a device remotely with PowerShell
# Trust the remote pi and connect to it
Write-Host "Configuring WinRM TrustedHosts..."
$trustedHosts = Get-Item WSMan:\localhost\Client\TrustedHosts
if ($trustedHosts.Value -eq "") {
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $ipAddress -Force
}
elseif (-not $trustedHosts.Value.Contains($ipAddress)) {
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "$($trustedHosts.Value),$($ipAddress)" -Force
}
Write-Host "Establishing PS Session to $deviceName..."
$pstimeout = New-PSSessionoption -OperationTimeout (1000*60*5)
$session = New-PSSession -computer $ipAddress -Credential $cred -ErrorAction Stop -SessionOption $pstimeout
#Rename Device to Device Name
Write-Host "Renaming Device $ipAddress to $deviceName"
Invoke-Command -Session $session -FilePath .\Rename-Device.ps1 -ArgumentList $deviceName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment