Hi
I am trying to delete a registry key from windows machines by invoke method. Is there any way to write the output for the cmdlet (Remove-ItemProperty) or any other alternate to know whether the key is deleted or not.
$csvpath=""
$Credential= Get-Credential -Message "Enter the password for Administrator User" -UserName "Administrator"
$script = @'
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
if([IntPtr]::Size -eq 8){
$text = Remove-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\" -Name 'AgentGUID' -Force -ErrorAction SilentlyContinue
%{"$($_.AgentGUID)"}
}
elseif([IntPtr]::Size -eq 4){
$text = Remove-ItemProperty -Path "HKLM:\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\" -Name 'AgentGUID' -Force -ErrorAction SilentlyContinue
}
$text
'@
foreach($vmsincsv in (Import-CSV -Path $csvpath -UseCulture)){
$vm = Get-VM $vmsincsv.name
Invoke-VMScript -VM $vm -GuestCredential $Credential -ScriptText $script -ScriptType Powershell -ErrorAction Stop | Select -ExpandProperty ScriptOutput
}
No, then the Try-Catch will not work, but try with Stop
if([IntPtr]::Size -eq 8){
$path = "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' -ErrorAction Stop |
Select-Object -ExpandProperty Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
elseif([IntPtr]::Size -eq 4){
$path = "HKLM:\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' -ErrorAction Stop |
Select-Object -ExpandProperty Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There is no Test- cmdlet for registry keys values, but you can create something similar with a Try-Catch construct.
Something like this for example (returns $true if the key is there, otherwise $false).
You can also return a text string instead of a Boolean
try {
Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\" -Name 'AgentGUID' |
Select-Object -ExpandProperty $Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tried passing like this to the guest vms ($script). But even after deleting the string value (std) its showing $true.
Can you please correct me.
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
if([IntPtr]::Size -eq 8){
$text = Remove-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\" -Name 'std' -Force -ErrorAction SilentlyContinue
%{"$($_.AgentGUID)"}
}
elseif([IntPtr]::Size -eq 4){
$text = Remove-ItemProperty -Path "HKLM:\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\" -Name 'std' -Force -ErrorAction SilentlyContinue
}
try {
Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\" -Name 'std' | Select-Object -ExpandProperty $Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
Output:
The paths are not the same for 32- and 64-bit platforms, the Try-Catch needs to user the correct path.
The ExpandProperty require the propertyname, not $value.
Are you sure that the registry key is named 'std'?
if([IntPtr]::Size -eq 8){
$path = "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' | Select-Object -ExpandProperty Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
elseif([IntPtr]::Size -eq 4){
$path = "HKLM:\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' | Select-Object -ExpandProperty Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for correction.
Yes the registry key with 'std' is created by me for testing and with this script i can able to delete the key but after deleting still its showing true. ideally it should show false right.
Can change this (Get-ItemProperty -Path $path -Name 'std') to (Get-ItemProperty -Path $path -Name 'std' -ErrorAction SilentlyContinue) as its give error in output.
Output:
No, then the Try-Catch will not work, but try with Stop
if([IntPtr]::Size -eq 8){
$path = "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' -ErrorAction Stop |
Select-Object -ExpandProperty Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
elseif([IntPtr]::Size -eq 4){
$path = "HKLM:\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' -ErrorAction Stop |
Select-Object -ExpandProperty Value -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
OK, I kept only stop.
Instead of (Select-Object -ExpandProperty Value) changed to (Select-Object -ExpandProperty 'std') then its showing as expected.
like if the std string is not present(deleted by Remove-ItemProperty) it showing false.
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
if([IntPtr]::Size -eq 8){
$path = "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' -ErrorAction Stop |Select-Object -ExpandProperty 'std' -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
elseif([IntPtr]::Size -eq 4){
$path = "HKLM:\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\"
Remove-ItemProperty -Path $path -Name 'std' -Force -ErrorAction SilentlyContinue
try {
Get-ItemProperty -Path $path -Name 'std' -ErrorAction Stop |Select-Object -ExpandProperty 'std' -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
So it's working?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes its working as expected after doing the changes.
Thanks for support Guru