如何使用不同的本地管理员计数运行 power shell 脚本

如何使用不同的本地管理员计数运行 power shell 脚本

在我的计算机中,我使用了 2 个本地管理员帐户,一个是内置帐户(Admin1),另一个是新创建的帐户(Admin2)。这两个帐户都是管理员组的成员。

我有一个 PowerShell 脚本,用于在我的 PC 中创建还原点,并且用户名和密码被硬编码到脚本中,如下所示,并且它运行良好。

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted" -Force

$hostname = get-content env:computername
    
$username = "$hostname\Admin1"
$password = "Admin1@123"

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword


$command="

vssadmin Resize ShadowStorage /On=C: /For=C: /MaxSize=5GB

Enable-ComputerRestore -drive 'c:\'

Checkpoint-Computer -Description 'Inno GT Restore Point' -RestorePointType MODIFY_SETTINGS

Get-ComputerRestorePoint

Read-Host '
Restore Point Creation Successful
Press Enter key to leave...'

"

Start-Process Powershell.exe -Credential $credential -ArgumentList "-Command & {$command}"

但是我需要使用另一个管理员帐户运行此脚本,并且我已更改凭据,如下所示。

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted" -Force
    
    $hostname = get-content env:computername
        
    $username = "$hostname\Admin2"
    $password = "Admin2@123"
    
    $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
    
    $credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
    
    
    $command="
    
    vssadmin Resize ShadowStorage /On=C: /For=C: /MaxSize=5GB
    
    Enable-ComputerRestore -drive 'c:\'
    
    Checkpoint-Computer -Description 'Inno GT Restore Point' -RestorePointType MODIFY_SETTINGS
    
    Get-ComputerRestorePoint
    
    Read-Host '
    Restore Point Creation Successful
    Press Enter key to leave...'
    
    "
    
    Start-Process Powershell.exe -Credential $credential -ArgumentList "-Command & {$command}"

但是它出现了如下错误:

在此处输入图片描述

我不是 PowerShell 专家,所以我将Start-Process部分内容进行了如下更改

Start-Process Powershell.exe -Verb RunAs -Credential $credential -ArgumentList "-Command & {$command}"

但此后,我又收到如下错误:

在此处输入图片描述

答案1

继续我的评论...(总是提示信用)

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted" -Force

$Creds      = Get-Credential -Credential "$env:COMPUTERNAME\$env:USERNAME"

$command="
vssadmin Resize ShadowStorage /On=C: /For=C: /MaxSize=5GB

Enable-ComputerRestore -drive 'c:\'

Checkpoint-Computer -Description 'Inno GT Restore Point' -RestorePointType MODIFY_SETTINGS

Get-ComputerRestorePoint

Read-Host '
Restore Point Creation Successful
Press Enter key to leave...'
"
Start-Process Powershell.exe -Credential $Creds  -ArgumentList "-Command & {$command}" 

更新

...并跟进我上次对您所述用例的评论。

這一行...

Start-Process Powershell.exe -Verb RunAs -Credential $credential -ArgumentList "-Command & {$command}"

...应该这样写才能工作...

Start-Process powershell -Credential $credential -ArgumentList '-NoProfile -Command &{Start-Process cmd.exe -verb runas}'

或者尝试...

Start-Process powershell -ArgumentList "-NoExit","-NoProfile", "-Command  &{ cmd.exe }" -RunAs $credential

根据我上次的评论更新

Function Start-ConsoleCommand
{
    [CmdletBinding(SupportsShouldProcess)]

    [Alias('scc')]

    Param  
    ( 
        [string]$ConsoleCommand,
        [switch]$PoSHCore
    )

    If ($PoSHCore)
    {Start-Process pwsh -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}
    Else
    {Start-Process powershell -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}

}

根据需要进行调整。

相关内容