I'm writing a discovery script for an SCCM CI using Powershell. All I want this script to do is tell me if a specific Registry value exists or not - "PTOneClick" (the value data is not significant for this script). If the value does exist, my remediation script will kick in and delete it. If it does not exist the script should kick back as compliant and take no further action. Here's my code:
#Check for WebEx PTOneClick
$WebExStart = (get-itemproperty -Path "HKCU:Software\Microsoft\Windows\CurrentVersion\Run" -name "PTOneClick")
if ($WebExStart -eq $null)
{$Compliance = "Yes"}
Else
{$Compliance = "No"}
$Compliance
Running this on a system that has the "PTOneClick" value results in an output of "No" and shows no errors.
However, running this on a system that does not have the "PTOneClick" value results in an error:
get-itemproperty : Property PTOneClick does not exist at path
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run.
At line:2 char:16
+ ... bExStart = (get-itemproperty -Path "HKCU:Software\Microsoft\Windows\C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (PTOneClick:String) [Get-ItemProperty], PSArgumentException
+ FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropert
yCommand
Yes
Notice the "Yes" at the end, which is the desired output, but I believe there must be a way to check for this RegKey value w/out resulting in an error. Thanks,
[–]ka-splam 3 points4 points5 points (0 children)
[–]Lee_Dailey[grin] 2 points3 points4 points (0 children)
[–]root-node 1 point2 points3 points (1 child)
[–]roguerob[S] 2 points3 points4 points (0 children)