将Powershell命令运行到需要管理员权限的其他ws

时间:2017-11-16 04:24:09

标签: powershell

我需要运行TPM命令(需要管理员访问权限)。

让我们将这些用于图例:

  1. Angelo - 标准用户
  2. AngeloAdmin - 管理员用户
  3. Windows7 - 其中包含标准用户的计算机
  4. 如何使用我的标准帐户以管理员身份运行脚本,以使用标准帐户远程执行脚本到另一台计算机?

    继承我将运行的部分代码:

    Set-Variable -Name BuildLog -Scope Global -Force
    Set-Variable -Name Errors -Value $null -Scope Global -Force
    Set-Variable -Name LogFile -Scope Global -Force
    Set-Variable -Name Phase -Scope Global -Force
    Set-Variable -Name RelativePath -Scope Global -Force
    Set-Variable -Name Sequence -Scope Global -Force
    Set-Variable -Name Title -Scope Global -Force
    
    Function ConsoleTitle ($Title){
        $host.ui.RawUI.WindowTitle = $Title
    }
    
    Function DeclareGlobalVariables {
        $Global:BuildLog = $Env:windir+"\Logs\BuildLogs\Build.csv"
        $Global:LogFile = $Env:windir+"\Logs\BuildLogs\TPM_On.log"
        $Global:Phase = "Final Build"
        $Global:Sequence = ""
        $Global:Title = "TPM Clear Ownership"
    }
    
    Function GetRelativePath { 
        $Global:RelativePath = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)+"\" 
    }
    
    Function ClearTPM {
        #Declare Local Memory
        Set-Variable -Name ClassName -Value "Win32_Tpm" -Scope Local -Force
        Set-Variable -Name Computer -Value $env:COMPUTERNAME -Scope Local -Force
        Set-Variable -Name NameSpace -Value "ROOT\CIMV2\Security\MicrosoftTpm" -Scope Local -Force
        Set-Variable -Name oTPM -Scope Local -Force
    
        $oTPM = Get-WmiObject -Class $ClassName -ComputerName $Computer -Namespace $NameSpace
        $Output = "Clearing TPM Ownership....."
        Write-Host "Clearing TPM Ownership....." -NoNewline
        $Temp = $oTPM.SetPhysicalPresenceRequest(5)
        If ($Temp.ReturnValue -eq 0) {
            $Output = "Success"
            Write-Host "Success" -ForegroundColor Yellow
        } else {
            $Output = "Failure"
            Write-Host "Failure" -ForegroundColor Red
            $Global:Errors++
        }
        Out-File -FilePath $Global:LogFile -InputObject $Output -Append -Force
    
        #Cleanup Local Memory
        Remove-Variable -Name oTPM -Scope Local -Force
    }
    
    Function ProcessLogFile {
        If ((Test-Path $Env:windir"\Logs") -eq $false) {
            New-Item -ItemType Directory -Path $Env:windir"\Logs"
        }
        If ((Test-Path $Env:windir"\Logs\ApplicationLogs") -eq $false) {
            New-Item -ItemType Directory -Path $Env:windir"\Logs\ApplicationLogs"
        }
        If ((Test-Path $Env:windir"\Logs\BuildLogs") -eq $false) {
            New-Item -ItemType Directory -Path $Env:windir"\Logs\BuildLogs"
        }
        If ($Global:Errors -eq $null) {
            If (Test-Path $Global:LogFile) {
                Remove-Item $Global:LogFile -Force
            }
            $File1 = $Global:LogFile.Split(".")
            $Filename1 = $File1[0]+"_ERROR"+"."+$File1[1]
            If (Test-Path $Filename1) {
                Remove-Item $Filename1 -Force
            }
            $Global:Errors = 0
        } elseIf ($Global:Errors -ne 0) {
            If (Test-Path $Global:LogFile) {
                $Global:LogFile.ToString()
                $File1 = $Global:LogFile.Split(".")
                $Filename1 = $File1[0]+"_ERROR"+"."+$File1[1]
                Rename-Item $Global:LogFile -NewName $Filename1 -Force
            }
        } else {
            $date = get-date
            $LogTitle = $Global:Phase+[char]9+$Global:Sequence+[char]9+$Global:Title+[char]9+$date.month+"/"+$date.day+"/"+$date.year+" "+$date.hour+":"+$date.minute
            Out-File -FilePath $Global:BuildLog -InputObject $LogTitle -Append -Force
        }
    }
    
    Function ExitPowerShell {
        If (($Global:Errors -ne $null) -and ($Global:Errors -ne 0)) {
            Exit 1
        }
    }
    
    cls
    GetRelativePath
    DeclareGlobalVariables
    ConsoleTitle $Global:Title
    ProcessLogFile
    ClearTPM
    ProcessLogFile
    Start-Sleep -Seconds 5
    ExitPowerShell
    

1 个答案:

答案 0 :(得分:0)

如果您想以不同的用户身份运行脚本,可以' SHIFT +右键单击>以其他用户身份运行'但这仅适用于应用程序,因此您必须以不同的用户身份运行Powershell,然后运行脚本,您可以创建批处理文件来执行此操作。以下是批处理文件中需要的示例。

runas /user:yourdomain.com\administrator powershell

如果您没有域名,请使用计算机名称

runas /noprofile /user:computername\administrator powershell