2

Consider the following PowerShell code:

IF(Test-Path -Path "C:\Windows\System32\File123")
{Remove-Item -Force -Path "C:\Windows\System32\File123"}

If the code was executed in the x86 PowerShell Console, the following error is raised Get-ChildItem : Cannot find path 'C:\Windows\System32\File123' because it does not exist.

However, when the code is run in an x64 Powershell console, the command behaves as expected.

Is there any scriptable method for working around this problem?

1
  • 1
    You can use $env:Processor_Architecture as displayed in the answer to find out if Powershell is x86 or x64. Then, you can select proper system32 choosing from c:\windows\sysWOW64 (x86's under x64 env), c:\windows\sysnative (x64's from under x86) or c:\windows\system32 (native for each env). Commented Jul 28, 2015 at 7:54

2 Answers 2

2

A hacky workaround would be to detect that the script is run with PowerShell x86 and invoke it with a x64 PowerShell by putting this snippet at the start of your script:

if ($env:Processor_Architecture -eq "x86")
{
    &"$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe" -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass 
    exit
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think -website is an unneeded parameter, but thanks for sysnative to access x64's system32 from x86.
Thank you Vesper, you are right, I copied that snippet from one of my script which takes a -WebSite paramet. I remove it.
Thanks for this, I posted this question at the end of my shift. I've had some time to properly look into this and came up with something that appears to do the trick.
0

Thanks for the great replies, I had some time to do a little searching and came up with the following. I'm using Powershell v4. You can put anything after the ps64 alias, a script for example or a function. Credit for the alias goes to this page. Also thanks to https://stackoverflow.com/a/19835164/4317867 for the [ScriptBlock]::Create("") tip, before doing this the Script block would not expand $server properly.

The goal of this is to remove Powershell's scheduled task/job file to allow it to be re-created.

Param(
 [Parameter(Mandatory=$true,
  HelpMessage="ServerName goes here")]
[string]$server,
 [Parameter(Mandatory=$true,
  HelpMessage="Enter a Date/Time 07-28-15 16:00 For July 28th, 2015 at 4:00 PM")]
  [ValidatePattern('\d{2}-\d{2}-\d{2}\s\d{2}[:]\d{2}')]    
$date)

if($env:PROCESSOR_ARCHITECTURE -eq "x86")
{
 set-alias ps64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe"
 ps64 -command "IF(Test-Path -Path C:\Windows\System32\Tasks\Microsoft\Windows\PowerShell\ScheduledJobs\RebootOnce2){Remove-Item -Force -Path C:\Windows\System32\Tasks\Microsoft\Windows\PowerShell\ScheduledJobs\RebootOnce2}" #End 64 bit powershell.
}
Else{
 Get-ScheduledJob | Unregister-ScheduledJob
}
$user = Get-Credential -UserName $env:USERNAME -Message "UserName/password for scheduled Reboot"
$trigger = New-JobTrigger -once -at $date
$script = [ScriptBlock]::Create("D:\Scripts\Scheduled-Reboot-Single.ps1 -server $server | Out-File -Force \\LogServer\d$\scripts\$server-Reboot.log")
Register-ScheduledJob -Name RebootOnce2 -Credential $user -Trigger $trigger -ScriptBlock $script

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.