Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在if块内无法识别$Env变量_Powershell - Fatal编程技术网

Powershell 在if块内无法识别$Env变量

Powershell 在if块内无法识别$Env变量,powershell,Powershell,我正在尝试执行这段代码,它基本上只是通过网络将一个文件复制到本地登录用户的桌面上,如果它比网络上的旧的话。下面代码的第一行运行良好,但对于在if块中使用$env:userprofile的部分,它会抛出一个错误。不知道这里发生了什么 Copy-Item -Path "\\path1\subpath1\subpath2\Patch\help\*" -Filter *.chm -Destination "$Env:UserProfile\Desktop" -force -Recurse $chmfi

我正在尝试执行这段代码,它基本上只是通过网络将一个文件复制到本地登录用户的桌面上,如果它比网络上的旧的话。下面代码的第一行运行良好,但对于在if块中使用$env:userprofile的部分,它会抛出一个错误。不知道这里发生了什么

Copy-Item -Path "\\path1\subpath1\subpath2\Patch\help\*" -Filter *.chm -Destination "$Env:UserProfile\Desktop" -force -Recurse

$chmfileNetwork = Get-ItemPropertyvalue -Path 'path1\subpath1\subpath2\Patch\help\*' -Filter *.chm -Name 'LastWriteTime'

$chmfileLocal = Get-ItemPropertyValue -Path '$Env:UserProfile\Desktop\*' -Filter *.chm -Name 'LastWriteTime'

if ($chmfileLocal -lt $chmfileNetwork) {
    Copy-Item -Path "path1\subpath1\subpath2\Patch\help\*" -Destination "$Env:UserProfile\Desktop" -force -Recurse
} else {
    echo "Saul good, man"
}
这就产生了错误

Get-ItemPropertyValue : Cannot find drive. A drive with the name '$Env' does not exist. At C:\Users\user1\Downloads\PS2EXE-GUI\psfile.ps1:28 char:17 + ... fileLocal = Get-ItemPropertyValue -Path '$Env:UserProfile\Desktop\*' ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: ($Env:String) [Get-ItemPropertyValue], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetItemPropertyValueCommand 您可以使用$home\Desktop获取用户的桌面文件夹,但Ansgar Wiechers确实指出了脚本的问题,说明了它抛出错误的原因。您在chmfileLocal=行中使用了单引号。使用单引号时,变量不会展开,只有使用双引号时才会展开。原始脚本可以通过以下更改进行修复:

$chmfileLocal = Get-ItemPropertyValue -Path "$Env:UserProfile\Desktop\*" -Filter *.chm -Name 'LastWriteTime'
您可以使用$home\Desktop获取用户的桌面文件夹,但Ansgar Wiechers确实指出了脚本的问题,说明了它抛出错误的原因。您在chmfileLocal=行中使用了单引号。使用单引号时,变量不会展开,只有使用双引号时才会展开。原始脚本可以通过以下更改进行修复:

$chmfileLocal = Get-ItemPropertyValue -Path "$Env:UserProfile\Desktop\*" -Filter *.chm -Name 'LastWriteTime'

试试$home\Desktop而不是谢谢MadTechnician,他们现在会试试,让你知道这是否有效。嘿,这很有效。非常感谢你。你能把它作为一个答案吗?“$Env:UserProfile\Desktop\*”->$Env:UserProfile\Desktop\*试试$home\Desktop代替它谢谢技术员,现在会试试,告诉你是否行得通。嘿,行得通。非常感谢你。您能将此添加为答案吗?“$Env:UserProfile\Desktop\*”->$Env:UserProfile\Desktop\*非常感谢MadTechnician和Ansgar Wiechars的回答。非常感谢MadTechnician和Ansgar Wiechars的回答。