我寫一個腳本,將經過服務器的列表,並確認他們是在與NTP同步運行W32TM。設施操作員要求,腳本應作爲普通腳本運行,並且如果目標不同步,則應請求管理員憑證。我們不幸的是現在不能改變NTP配置,所以我們必須做出解決方法。如何在未提升的PowerShell
我現在擁有的(如果腳本以管理員身份運行,它的工作原理非常漂亮)是通過「Invoke-Command」執行的命令(遠程計算機的「w32tm/query/status」),所以我可以通過它管理員憑據。
我的想法是避免使用WinRM,因爲主機名解析在我們的系統中無法正常工作(它需要一些痛苦的主機到IP後退到正確的主機名解析),這使得WinRM無用。
命令W32TM可以獲得在遠程計算機的狀態,但它需要被運行作爲的管理員。
在這兩種情況下(以管理員身份運行並以普通用戶身份運行,然後提供憑據),$腳本作爲域\管理員執行(通過檢查Admin角色和「WhoAmI」命令進行確認),但狀態爲只有在整個腳本以管理員身份執行時才能獲得。 對於執行作爲普通用戶收到錯誤:
The following error occurred: Access is denied. (0x80070005)
所有的機器我使用明顯允許遠程執行,因爲它具有管理員用戶工作。
所以基本上我的問題是,爲什麼是「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
}
}
我看你檢查,看看如果當前用戶是管理員,但我從來沒有看到你檢查,看看傳遞的憑證是否是管理員信用。那是否處理了其他地方? – TheMadTechnician
我不檢查密碼。我從來沒有想過,因爲我認爲,因爲我作爲預期的用戶運行完整的憑據是正確的。我認爲,因爲這個用戶(管理員)沒有他們不能登錄。 – Luka
你誤解了我的評論。我看到你的函數接受$ cred,並在'invoke-command -computername「localhost」-Credential $ cred'中使用該變量,但是我沒有看到你檢查這些憑據的位置,看看它們是否是管理員。 – TheMadTechnician