I'm new to coding trying to figure out powershell and confused at If statements. Trying to get this code so it'll create the job scheduling to run on login but it won't create it on the initial first run. I want to add in an if statement at the end to run the command to check if it exists on a desktop as some end users I want to deploy this too will already have it. But I'm unsure where the error lies with the code and how to address or simplify the code? Any help would be greatly appreciated
$PathTest = Test-Path -Path "C:\Users\public\desktop\Google.lnk"
$Google = {$ShortcutPath = "C:\users\public\desktop\Google.lnk"
$IconLocation = "C:\windows\System32\SHELL32.dll"
$IconArrayIndex = 150
$Shortcut = $Shell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = "http://www.google.com"
$Shortcut.IconLocation = "$IconLocation, $IconArrayIndex"
$Shortcut.Save()
}
$Trigger = New-JobTrigger -AtLogOn -RandomDelay 00:00:30
Register-ScheduledJob -Trigger $Trigger -RunAs32 -ScriptBlock {$Google} -Name Google
If ($PathTest -is True) {
$PathTest
} Else {
$Google
}
CodePudding user response:
You use a wrong if operator:
-is
is used for checking types:
if ( $value -is [type] )
{
# do something
}
For a boolean check you can just use:
$condition = $true
if ( $condition ) # check for $true
{
Write-Output "The condition was true"
}
$condition = $false
if ( !$condition ) # check for $false
{
Write-Output "The condition was also true by inverting with not operator"
}
MS Docs PowerShell IF-Statement
CodePudding user response:
The if
statement is using wrong operator. -is
is used to test object types, not logical equality which is -eq
. In addition, the boolean true is $true