Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在非提升powershell中运行w32tm_Powershell_Powershell 2.0_Ntp - Fatal编程技术网

如何在非提升powershell中运行w32tm

如何在非提升powershell中运行w32tm,powershell,powershell-2.0,ntp,Powershell,Powershell 2.0,Ntp,我正在编写一个助手脚本,它将遍历服务器列表,并验证它们是否与NTP同步。应设施运营商的要求,该脚本应作为正常脚本运行,如果目标不同步,则应请求管理员凭据。不幸的是,我们目前无法更改NTP配置,因此必须进行变通 我现在拥有的(如果脚本以管理员身份运行,效果会非常好)是一个命令(“远程计算机的w32tm/query/status”),它通过“Invoke command”执行,因此我可以向它传递管理员凭据 我的想法是避免使用WinRM,因为主机名解析在我们的系统中无法正常工作(这需要一些痛苦的主机到

我正在编写一个助手脚本,它将遍历服务器列表,并验证它们是否与NTP同步。应设施运营商的要求,该脚本应作为正常脚本运行,如果目标不同步,则应请求管理员凭据。不幸的是,我们目前无法更改NTP配置,因此必须进行变通

我现在拥有的(如果脚本以管理员身份运行,效果会非常好)是一个命令(“远程计算机的w32tm/query/status”),它通过“Invoke command”执行,因此我可以向它传递管理员凭据

我的想法是避免使用WinRM,因为主机名解析在我们的系统中无法正常工作(这需要一些痛苦的主机到IP并返回到正确的主机名解析),这使得WinRM毫无用处

w32tm命令可以获取远程计算机的状态,但需要以管理员身份运行

在这两种情况下(以管理员身份运行和以普通用户身份运行,稍后提供凭据),$script都以domain\administrator身份执行(通过检查Admin角色和“WhoAmI”命令确认),但只有当整个脚本以管理员身份执行时,才会获得状态。 对于作为普通用户执行,我收到错误:

The following error occurred: Access is denied. (0x80070005)
我使用的所有机器显然都允许远程执行,因为它与管理员用户一起工作

因此,基本上我的问题是,如果用户角色对于任务是合适的(它是管理员角色),为什么$script中不允许使用“w32tm…”命令

脚本中我无法解析的部分:

function synchronize_remote_machine ($target, $username, $cred)
{
    $script = { 
        param( [String] $compName )

        $user = [Security.Principal.WindowsIdentity]::GetCurrent();
        $userIsAdmin = (New-Object Security.Principal.WindowsPrincipal $user).`
                IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)  

        if (-not $userIsAdmin)
        {
            Write-Warning "You do not have Administrator rights to run this script!`n
                    Please re-run this script as an Administrator!"
        }
        else    
        {
            whoAmI
            w32tm /query /status /computer:$compName
            #w32tm /resync /rediscover /computer:$compName
        }
    }

    # resync via WinRM
    try 
    {
        #execute_resync_command $target $username $cred
        if ($username -eq 'Administrator')
        {
            # when run as admin
            invoke-command -ScriptBlock $script -ArgumentList $target;
        }
        else
        {
            # for normal user the initalized credential cals is used
            invoke-command -computername "localhost" -Credential $cred -ScriptBlock $script -ArgumentList $target
        }
    }
    catch
    {
        Write-Error "Error executing resync command on host $target."# -foregroundcolor "red"
        throw
    }   
}

即使您以管理员身份执行脚本,您是否希望尝试在提升的进程中运行脚本

您可以使用
Start Process
CmdLet实现这一点

start-process 'c:\system32\WindowsPowerShell\v1.0\powershell.exe' -verb runas -argumentlist "-file YourScript.ps1"
我将在这些服务器上授予操作员组
SeSystemtimePrivilege
,而不是以提升的权限(重新)运行脚本。您可以使用组策略或通过在每台服务器上从运行
ntrights.exe
来执行此操作:

ntrights +r SeSystemtimePrivilege -u DOMAIN\operators

我看到您正在检查当前用户是否是管理员,但我从未看到您检查传递的凭据是否是管理员凭据。那是在别的地方处理的吗?我没有检查密码。我从来没有想过这一点,因为我假设,因为我是作为预期用户运行的,所以完整的凭据是正确的。我想是因为这个用户(管理员)没有他们就无法登录。你误解了我的评论。我看到您的函数接受$cred,并在
invoke命令-computername“localhost”-Credential$cred
中使用该变量,但我看不出您在哪里检查这些凭据以确定它们是否是管理员。是的,我误解了您的问题。我发现,我的凭据实际上不是运行所需命令所需的管理员凭据。为什么我(还)不知道。如果我运行
whois/all
用户的SID与“以管理员身份运行”powershell中的相同,但权限不同。很抱歉,对于我所指的命令
whoAmI/all
。我遵循了您的建议,但遇到了与不允许该命令相同的阻塞问题。据我所知,提供给
$script
的凭据似乎不正确,并且没有给我正确的权限。因此,powershell没有以正确的权限执行,w32tm命令失败。这是我向IT人员提供的一个很好的建议,但目前我们无法(允许)对环境进行任何更改(由于监管问题)。不幸的是,我需要一个解决这个问题的方法。