Automation

 View Only
  • 1.  invoke script output

    Posted May 04, 2018 08:22 AM

    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

    }



  • 2.  RE: invoke script output

    Posted May 04, 2018 08:55 AM

    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

    }



  • 3.  RE: invoke script output

    Posted May 04, 2018 09:29 AM

    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:



  • 4.  RE: invoke script output

    Posted May 04, 2018 09:38 AM

    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 

        }

      



  • 5.  RE: invoke script output

    Posted May 04, 2018 10:10 AM

    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:



  • 6.  RE: invoke script output
    Best Answer

    Posted May 04, 2018 10:54 AM

    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

        }

    }

     



  • 7.  RE: invoke script output

    Posted May 04, 2018 12:29 PM

    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

        }

    }



  • 8.  RE: invoke script output

    Posted May 04, 2018 01:21 PM

    So it's working?



  • 9.  RE: invoke script output

    Posted May 04, 2018 02:45 PM

    yes its working as expected after doing the changes.

    Thanks for support Guru:smileyhappy: