Reputation: 11
I've been working on and off on a project to compress and move files older than x days to an archival folder. I've retrofitted the script here: https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Compress-Log-121e63b5 to assist me however I'm running into an issue that has proved fairly annoying.
When I run this on my machine, utilizing local directories, the script completes as expected. However, when I pass networked file paths to the script, the Get-WmiObject
query begins returning null results.
For example, this is a sample command line that works:
powershell -executionpolicy remotesigned -File compress_and_move_files.ps1 c:\temp\ c:\temp\compress_test\ 14
When I move to a UNC path, I begin getting the null-valued expression error on the WMIQuery.Compress() call
powershell -executionpolicy remotesigned -File compress_and_move_files.ps1 \\server1\temp\ \\server1\temp\compress_test\ 14
This is the full error:
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
Upvotes: 1
Views: 694
Reputation: 174445
That script attempts to retrieve a CIM_DataFile
instance - a class that isn't accessible via UNC paths in WMI.
Change the script to target the remote computer and then use the local file system path:
$Server = "server1"
$WMIFile = "C:\temp\".Replace("\", "\\")
$WMIQuery = Get-WmiObject -Computer $Server -Query "SELECT * FROM CIM_DataFile WHERE Name='$WMIFileName'"
Upvotes: 2