Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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空指针异常_Powershell_Wmi - Fatal编程技术网

Powershell空指针异常

Powershell空指针异常,powershell,wmi,Powershell,Wmi,我一直在做一个项目,将超过x天的文件压缩并移动到存档文件夹。我在这里修改了脚本:为了帮助我,我遇到了一个相当恼人的问题 当我在我的机器上使用本地目录运行此脚本时,脚本将按预期完成。但是,当我将网络文件路径传递给脚本时,Get WmiObject查询开始返回空结果 例如,这是一个示例命令行: powershell -executionpolicy remotesigned -File compress_and_move_files.ps1 c:\temp\ c:\temp\compress_tes

我一直在做一个项目,将超过x天的文件压缩并移动到存档文件夹。我在这里修改了脚本:为了帮助我,我遇到了一个相当恼人的问题

当我在我的机器上使用本地目录运行此脚本时,脚本将按预期完成。但是,当我将网络文件路径传递给脚本时,
Get WmiObject
查询开始返回空结果

例如,这是一个示例命令行:

powershell -executionpolicy remotesigned -File compress_and_move_files.ps1 c:\temp\ c:\temp\compress_test\ 14
当我移动到UNC路径时,我开始在WMIQuery.Compress()调用中获得空值表达式错误

这是全部错误:

You cannot call a method on a null-valued expression.
At compress_and_move_files.ps1:14 char:23
+ If ($WMIQuery.Compress <<<< ()) {Write-Host "$FullName compressed successfull
y."-ForegroundColor Green}
    + CategoryInfo          : InvalidOperation: (Compress:String) [], RuntimeE
   xception
    + FullyQualifiedErrorId : InvokeMethodOnNull
不能对空值表达式调用方法。
在压缩和移动文件时。ps1:14字符:23

+如果($WMIQuery.Compress该脚本尝试检索实例,则该实例是无法通过WMI中的UNC路径访问的类

将脚本更改为以远程计算机为目标,然后使用本地文件系统路径:

$Server   = "server1"
$WMIFile  = "C:\temp\".Replace("\", "\\")
$WMIQuery = Get-WmiObject -Computer $Server -Query "SELECT * FROM CIM_DataFile WHERE Name='$WMIFileName'"
$Server   = "server1"
$WMIFile  = "C:\temp\".Replace("\", "\\")
$WMIQuery = Get-WmiObject -Computer $Server -Query "SELECT * FROM CIM_DataFile WHERE Name='$WMIFileName'"