无法使用Powershell删除某些文件和文件夹

无法使用Powershell删除某些文件和文件夹,powershell,Powershell,我需要创建一个脚本来清除远程计算机上的Lync缓存。为此,我需要关闭Outlook和Lync(如果它们处于打开状态),然后清除两个文件夹,同时删除另一个文件夹。以下是我到目前为止的情况: [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null $computer = [Microsoft.VisualBasic.Interaction]::InputBox("Which compu

我需要创建一个脚本来清除远程计算机上的Lync缓存。为此,我需要关闭Outlook和Lync(如果它们处于打开状态),然后清除两个文件夹,同时删除另一个文件夹。以下是我到目前为止的情况:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$computer = [Microsoft.VisualBasic.Interaction]::InputBox("Which computer do you wish to clear the Lync cache for?", "Computer", "$env:computername")
$username = Get-WMIObject -class Win32_ComputerSystem -ComputerName $computer | select username

$RSA = "C$\Users\$username\AppData\Roaming\Microsoft\Crypto\RSA"
$Tracing = "C$\Users\$username\AppData\Local\Microsoft\Office\15.0\Lync\Tracing"
$SIP = "C$\Users\$username\AppData\Local\Microsoft\Office\15.0\Lync"


(Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match "Outlook" }).Terminate()
(Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match "Lync" }).Terminate()
(Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match "Communicator" }).Terminate()

start-sleep -s 3

Get-ChildItem -path \\$computer\$RSA -include * | Remove-Item -recurse -force
Get-ChildItem -path \\$computer\$Tracing -include * | Remove-Item -recurse
Remove-Item -path \\$computer\$SIP\*sip* -recurse -force
脚本根据需要关闭程序,但似乎对文件夹没有任何作用。我得到的唯一错误如下:

You cannot call a method on a null-valued expression.
At C:\Users\*myusername*\Desktop\Personal Powershell scripts\Clear Lync cache.ps1:10 char:1
+ (Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\*myusername*\Desktop\Personal Powershell scripts\Clear Lync cache.ps1:11 char:1
+ (Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\*myusername*\Desktop\Personal Powershell scripts\Clear Lync cache.ps1:12 char:1
+ (Get-WmiObject Win32_Process -ComputerName $computer | ?{ $_.ProcessName -match  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

ISE以管理员身份开放。我拥有该域以及相关文件夹的管理员权限。有人知道我哪里出错了吗?

为$username返回的值是多少?我怀疑这里面有这个域,它必须被解析出来。我想你是对的。我对它做了一点修改,但它仍然返回域名
$username=Get WMIObject-class Win32\u ComputerSystem-ComputerName$computer | select-expand username
我想你会想要这样的东西:
(Get WMIObject-class Win32\u ComputerSystem-ComputerName$computer)。username.Split(“\”[1]
看起来WMI没有返回任何东西,然后你调用terminate()在一个空对象上Nathan Rice这正是我想要的。现在很好用。非常感谢。