RegKey Value in to a variable in PowerShell - powershell

I have the below code snippet:
$Reg2 = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Machine)
$RegKey2= $Reg.OpenSubKey("SOFTWARE\\TrendMicro\\PC-cillinNTCorp\\CurrentVersion\\Misc.")
$CheckAVVer = $RegKey2.GetValue("ProgramVer")
Whats happening is $CheckAVVer doesn't seem to grab the value. If remove the $CheckAVVer, it works and displays the value.
Please help.

In the second line, change:
$Reg.OpenSubKey("SOFT...
To:
$Reg2.OpenSubKey("SOFT...
And see if that fixes it for you.
$Reg2 = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Machine)
$RegKey2= $Reg2.OpenSubKey("SOFTWARE\\TrendMicro\\PC-cillinNTCorp\\CurrentVersion\\Misc.")
$CheckAVVer = $RegKey2.GetValue("ProgramVer")
Write-host $CheckAVVer

Related

Variable being passed in as null - Powershell

So I have an issue that has been bugging me for a few hours now.
I have two functions, Write-Log, and LogProfileRemoval. In Write-Log, I pass in the two arguments as shown here.
LogProfileRemoval('$LogEventDetail', 100000)
But when I check the variables of LogProfileRemoval they are shown like this
$LogEventDetail = '$LogEventDetail' 100000
$LogMethod = $null
I am aware that I have quotes around the variable $LogEventDetail, that was part of my testing to figure this out. Really that variable could be anything and it still concats those two variables into one and leaves the 2nd parameter as a null value.
What am I doing wrong here.
Thanks
function LogProfileRemoval($LogEventDetail, $LogMethod)
{
Switch ($LogMethod)
{
'EventLog' {LogToEventLog($LogEvent)}
}
}
function Write-Log($logDetail, $logEvent=2)
{
$LogEventDetail = New-Object EventLog -Property #{EventTimeStamp=(Get-Date);EventType=$logEvent;EventDetail=$logDetail}
$LogMethod = 1
LogProfileRemoval('$LogEventDetail', 100000)
}
So by not following best practices, was the issue. Weird becuase I have always wrote my powershell scripts like this. Always been a not fan of the Param way of doing it. I changed it to best practice way (sorta) and it worked great.
I would like to know why it didn't work though but overall It just goes to show me to quick being lazy and do it the right way.
Code working shown below
function LogProfileRemoval
{
param(
$LogEventDetail,
$LogMethod
)
Switch ($LogMethod)
{
'EventLog' {LogToEventLog($LogEvent)}
}
}
function Write-Log($logDetail,$logEvent=2)
{
$LogEventDetail = New-Object EventLog -Property #{EventTimeStamp=(Get-Date);EventType=$logEvent;EventDetail=$logDetail}
$LogMethod = 1
LogProfileRemoval -LogEventDetail $LogEventDetail -LogMethod 'EventLog'
}

Powershell update a variable using another variable reference

I'm trying to concatenate a variable reference inside another string but it keeps showing wrong values.
Basically I intend to update variable $arq value acording to variable $version, without having to reset $arq.
I'm using [ref] but not sure it's the best way to do so.
I've tryed the following so far.
$downloadSource = "\\domain.or.ip\folder"
$version = "0.0.0"
$arq = "file_name_$([ref]$version)`_filename_continuation.zip"
function UpdateVersion {
(Get-ChildItem -name "$downloadSource\file_name*").Split('_')[2]
}
$version = UpdateVersion
echo $version
echo $arq
Variable $version is being updated correctly.
But $arq is receiving the wrong value:
"file_name_System.Management.Automation.PSReference`1[System.String]_filename_continuation.zip"
I tried changing $arq as follows
$arq = "file_name_" + $(($versaoEsperada).value) + "`_filename_continuation.zip"
But the same wrong value is shown.
Would you guys help me, please?
Thanks

Why weird assignment from variable inside Powershell switch statement?

I'm a beginner at Powershell and am struggling to understand some syntax from some code I found on Github. I've read the docs on Powershell assignment, and on switch statements, and can't understand what is going on with the = $Yes and = $No in this code snippet:
Switch ($Prompt3) {
Yes {
Stop-EdgePDF
Write-Output "Edge will no longer take over as the default PDF viewer."; = $Yes
}
No {
= $No
}
}
I haven't been able to find any references to this kind of syntax, and it doesn't seem to do anything in the script. So why is it there?
UPDATE: This issue has been resolved.
Looks to me like the variable name that was getting the assignment was deleted in a change back in August.
$PublishSettings = $Yes
Was changed to:
= $Yes
And:
$PublishSettings = $No
Was changed to:
= $No
Looks like poor search and replace.
I've created an issue for the problem at GitHub.
There are many characters that are valid in a function (or variable) name; this includes the = symbol. What you're observing is a function or alias.
Examples:
# standard function
function =
{
return $args
}
# accessing the function: drive
${Function:=} = {
return $args
}
# defining a new alias
New-Alias -Name = -Value Get-Variable
# using the Alias attribute
function Test-Thing
{
[Alias('=')]
param()
return $args
}

Colon in Powershell

I try to run a script in PowerShell that starts a program with additional parameters.
$arrgs = "/sipuri:juerg.schuepbach#parl.admin.ch /multiplecalls:block"
& "BusyOnBusy.exe" $arrgs
It seems PowerShell don't get the argument block. I guess because of the :
I've tried the tings with {} but it's always the same error.
Powershell does not say it is an error. It's the program that says it is missing the block argument.
Thank for the help.
$psi = New-Object Diagnostics.ProcessStartInfo
$psi.Arguments = "/sipuri:juerg.schuepbach#parl.admin.ch /multiplecalls:block"
$psi.FileName = "X:\PathForProgram\BusyOnBusy.exe"
#$psi.UseShellExecute = $false
[void][Diagnostics.Process]::Start($psi)
The colon is the drive designation operator. If you want to pass the string liternally without the parser doing any interpretaion, just single-quote the string:
$arrgs = '/sipuri:juerg.schuepbach#parl.admin.ch /multiplecalls:block'
What if you try:
start-process BusyOnBusy.exe "/sipuri:juerg.schuepbach#parl.admin.ch","/multiplecalls:block"

PowerShell - SCOM PropertyBag, How to check if already added?

I want to add values to a PropertyBag.
How is it possible to check if the value is already in the PropertyBag?
I know one can use an array, list, etc. But how can I use the $bag/$api object to do this check?
$api = New-Object -comObject “MOM.ScriptAPI”
$bag = $api.CreatePropertyBag()
$bag.AddValue("TestValue1","1234")
I'm searching for something like this:
if($bag -match "TestValue1")
{"In the Bag!"}
But, unfortunately, it's not working.
I do not have SCOM on a server I can access, but could you do the following to get the bag contents and check against it?
$api = New-Object -comObject “MOM.ScriptAPI”
$bag = $api.CreatePropertyBag()
$bagContents = $api.Return($bag)