如何远程运行 Cleanmgr.exe? [英] How do you run Cleanmgr.exe remotely?

查看:40
本文介绍了如何远程运行 Cleanmgr.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在远程运行 Windows 10 上的 Clean Manager 时遇到了一些问题.我看到了一些不同的事情,你可以编辑注册表并将 /sageset/sagerun 修改为特定的东西,然后远程运行它,但似乎无关紧要我做什么 CleanMgr 在我的机器上本地运行,而不是远程运行.

I'm having some trouble getting Clean Manager on Windows 10 to run remotely. I've seen a few different things were you can edit the registry and modify the /sageset or /sagerun to be specific things then run it remotely, but it seems no matter what I do the CleanMgr runs locally on my machine rather than running remotely.

我相信这是我最接近让它远程运行的...不过它似乎仍然只是在我的机器上本地运行.

I believe this is the closest I've gotten to get it to run remotely... It seems to still just run locally on my machine though.

有什么想法吗?

(所有变量都在脚本的这一部分之前设置,这只是我坚持的一小部分)

( All variables are set before this portion of the script, this is just a small portion of what's going on that I'm stuck on )

## Starts cleanmgr.exe
Function Start-CleanMGR {
    Write-Host "Please provide your A-Account details to continue with this cleanup."
    $creds = Get-Credential
    Enter-PSSession -ComputerName $computername -Credential $creds
    try {
        $cleanmgr = Start-Process -Credential $creds -FilePath "C:\Windows\System32\cleanmgr.exe" -ArgumentList '/verylowdisk' -Wait -Verbose
        if ($cleanmgr) {
            Write-Host "Clean Manager ran successfully!                                                                   " -NoNewline -ForegroundColor Green
            Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black
        }
    }
    catch [System.Exception] {
        Write-host "Cleanmgr is not installed! To use this portion of the script you must install the following windows features:" -NoNewline -ForegroundColor DarkGray
        Write-host "[ERROR]" -ForegroundColor Red -BackgroundColor Black
    }
} Start-CleanMGR

推荐答案

PowerShell 始终在启动会话的用户的用户上下文中运行.这是设计使然.

PowerShell always runs in the user context of the user who started the session. This is by design.

您无法使用 PowerShell 远程运行基于 GUI 的应用程序.这是一个 Windows 安全边界.

You can not run a GUI based application remotely using PowerShell. It is a Windows security boundary.

要运行 GUI 应用程序,必须有人登录,并且您不能以登录用户的身份使用 PowerShell 运行代码.

To run GUI apps, someone must be logged on, and you cannot use PowerShell to run code as the logged-on user.

您也在提示输入信息,因此必须有人登录.

You are also prompting for info, so, someone must be logged on.

如果您希望用户获得隐私信息,那么您需要:

If you are expecting a user to prived info, then you need to:

  1. 创建脚本
  2. 将脚本部署到用户的机器或从中共享的文件可以运行
  3. 告诉用户如何做或创建一个他们会加倍的批处理文件单击以运行 PowerShell 脚本

将脚本设置为在登录时或在一天中的某个时间作为计划任务运行,作为用户凭据.

Set the script to run as a scheduled task at log on or at some point during the day, as the user credentials.

变量有作用域,你不能在远程上下文中使用局部变量,除非它们有作用域.

Variables have the scope and you cannot use local variables in a remote context unless they are scoped for that.

关于远程变量

使用局部变量

你可以在远程命令中使用局部变量,但变量必须在本地会话中定义.

You can use local variables in remote commands, but the variable must be defined in the local session.

从 PowerShell 3.0 开始,您可以使用 Using scope 修饰符来识别远程命令中的局部变量.

Beginning in PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command.

Using 的语法如下:

The syntax of Using is as follows:

$Using:<VariableName>

仍然,远程变量不是您在用例中会做的事情,因为您无法使用 PowerShell 进行本机操作.你需要一个像 MS SysInternals PSExec 这样的 3rdP 工具来运行代码远程作为登录用户.

Still, the remote variable is not something you will do in your use case since you cannot do what you are after natively with PowerShell. You'll need a 3rdP tool like MS SysInternals PSExec to run code remotely as the logged-on user.

使用 PsExec

用法:psexec [\computer[,computer2[,...] |@file]][-u 用户 [-ppsswd][-n s][-r 服务名][-h][-l][-s|-e][-x][-i [会话]][-c可执行文件 [-f|-v]][-w 目录][-d][-][-a n,n,...] cmd[参数]

Usage: psexec [\computer[,computer2[,...] | @file]][-u user [-p psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c executable [-f|-v]][-w directory][-d][-][-a n,n,...] cmd [arguments]

-i 运行程序,使其与远程系统上指定会话的桌面交互.如果没有指定会话进程在控制台会话中运行.

-i Run the program so that it interacts with the desktop of the specified session on the remote system. If no session is specified the process runs in the console session.

-u 指定用于登录远程计算机的可选用户名.

-u Specifies optional user name for login to remote computer.

这篇关于如何远程运行 Cleanmgr.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆