英文:
Powershell script for cleaning up temp files
问题
Currently working on a script that prompts the user to type a machine name in and then Remote-PSSession onto the machine and delete all the temp files.
$computer = Read-Host -Prompt "Input the machine name"
Enter-PSSession -ComputerName $computer
$tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
Remove-Item $tempfolders -force -recurse
All that happens when running the above is it tries to find "D Drive" on the host machine rather than on the expected machine, suggesting it doesn't run the Enter-PSSession command.
英文:
Currently working on a script that prompts the user to type a machine name in and then Remote-PSSession onto the machine and delete all the temp files.
$computer = Read-Host -Prompt "Input the machine name"
Enter-PSSession -ComputerName $computer
$tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
Remove-Item $tempfolders -force -recurse
All that happens when running the above is it tries to find "D Drive" on the host machine rather than on the expected machine suggesting it doesn't run the Enter-PSSession command
答案1
得分: 3
Enter-PSSession
适用于交互式使用!
对于无人值守脚本,请使用 Invoke-Command
:
$computer = Read-Host -Prompt "输入计算机名称"
Invoke-Command -ComputerName $computer -ScriptBlock {
$tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
Remove-Item $tempfolders -Force -Recurse
}
英文:
Enter-PSSession
is for interactive use!
Use Invoke-Command
for unattended scripts:
$computer = Read-Host -Prompt "Input the machine name"
Invoke-Command -ComputerName $computer -ScriptBlock {
$tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
Remove-Item $tempfolders -Force -Recurse
}
答案2
得分: 0
这不是你可以使用 PowerShell 会话在远程机器上运行命令的方法。
请按照以下步骤:
步骤 1:创建一个远程会话并将其存储在一个变量中:
$Session = New-PSSession -ComputerName server1 -Credential (Get-Credential -UserName domain\username -Message "输入凭据")
步骤 2:使用以下命令行来调用命令:
Invoke-Command -Session $Session -ScriptBlock { <# 你想要运行的命令 #> }
如果该命令行应该产生任何输出,你可以将其分配给任何变量并使用它。
英文:
This is not how you can run commands on a remote machine with PowerShell Session.
Please follow this:
Step 1: Create a remote session and store it in a variable:
$Session = New-PSSession -ComputerName server1 -Credential (Get-Credential -UserName domain\username -Message "Enter credentials")
Step 2: Invoke the commands using below command line:
Invoke-Command -Session $Session -ScriptBlock { <# Commands that you want to run #> }
If that command line is supposed to give any output, you can assign it to any variable and use it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论