Powershell cannot find file

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 Get-ChildItem error occurs: Cannot find path "C: \ Windows \ System32 \ File123" because it does not exist.

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

Is there any scripting method to solve this problem?

+3


source to share


2 answers


The hacky workaround would be to detect that the script is run with x86 PowerShell and called with x64 PowerShell by putting this snippet at the beginning 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
}

      

+2


source


Thanks for the great answers, I had time to do some work and came up with the following. I am using Powershell v4. You can put anything after the ps64 alias like script or functions. Credit for the alias goes to this page . Also thanks to fooobar.com/questions/2248178 / ... for a hint [ScriptBlock]::Create("")

, before doing this, the script block will not decode $ server correctly.

The purpose of this is to remove the Powershell Job Schedule / File so that it can 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

      

0


source







All Articles