简体   繁体   中英

Have PowerShell create a shortcut based on OS bits

I'm tying to deploy a PowerShell script via a SCCM task sequence. As part of that I need to copy and rename a shortcut from the C:\\Program Files x86\\TESTapp\\test.exe directory. I have that done, however I was just informed that some client are on 32bit OS's.

What is the best way to have PowerShell determine what path to take, ie: "Program Files (x86)" or "Program Files"

Here is what I have that's currently not working:

Switch (Get-WmiObject Win32_OperatingSystem).OSArchitecture {
  "32-bit" {$PF = "Program Files"}
  "64-bit" {$PF = "Program Files x86"}
}

$TargetFile = "$env:C:\$PF\TESTapp\Test.exe"
$ShortcutFile = "$env:Public\Desktop\NewName-Test.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()

Any suggestions would be greatly appreciated.

Thank you!

Try this

Switch (Get-WmiObject Win32_OperatingSystem).OSArchitecture {
  "32-bit" {$PF = "$env:ProgramFiles"}
  "64-bit" {$PF = "$env:ProgramFiles(x86)"}
}
$TargetFile = "$PF\TESTapp\Test.exe"
If (Test-Path $TargetFile) {
    $ShortcutFile = "$env:Public\Desktop\NewName-Test.lnk"
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = $TargetFile
    $Shortcut.Save()
}

your mistake is - you forgot the () on the switch expression:

Switch ((Get-WmiObject Win32_OperatingSystem).OSArchitecture) {
  "32-bit" {$PF = "Program Files"}
  "64-bit" {$PF = "Program Files x86"}
}

$TargetFile = "C:\$PF\TESTapp\Test.exe"
$ShortcutFile = "$env:Public\Desktop\NewName-Test.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM