簡體   English   中英

Powershell 獲取當前的 DOMAIN OR COMPUTER NAME\USER 以及兩者的區別

[英]Powershell get current DOMAIN OR COMPUTER NAME\USER and the difference between the two

我正在制作一個腳本,用於將我的 API 注冊為 windows 服務。 由於我是 PS 新手,因此我在這里遵循了本指南並盡我所能填寫:

$acl = Get-Acl "$PSScriptRoot"
$aclRuleArgs = {DOMAIN OR COMPUTER NAME\USER}, "Read,Write,ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($aclRuleArgs)
$acl.SetAccessRule($accessRule)
$acl | Set-Acl "$PSScriptRoot"

New-Service -Name MyAPIService -BinaryPathName $PSScriptRoot\MyAPIService.exe -Credential {DOMAIN OR COMPUTER NAME\USER} -Description "API" -DisplayName "API Service" -StartupType Automatic

我想知道的是如何獲取當前域或計算機名稱,就像我使用 $PSScriptRoot 獲取當前目錄一樣。 該服務將在 Windows 10 上運行。
我也不知道我應該使用域還是計算機名\用戶? 在什么情況下我需要其中一種?

編輯:在@Patrick 的幫助下,我成功了,這是工作腳本:

$acl = Get-Acl "$PSScriptRoot"
$aclRuleArgs = "$env:COMPUTERNAME\$env:USERNAME", "Read,Write,ReadAndExecute", "ContainerInherit,ObjectInherit", "None", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($aclRuleArgs)
$acl.SetAccessRule($accessRule)
$acl | Set-Acl "$PSScriptRoot"

New-Service -Name MyAPIService -BinaryPathName $PSScriptRoot\MyAPIService.exe Description "API" -DisplayName "API Service" -StartupType Automatic

看看這里: 關於環境變量

$env:COMPUTERNAME
$env:USERNAME
$env:USERDNSDOMAIN

關於用戶:
是本地用戶還是域用戶? 如果是本地的,請使用“COMPUTERNAME\USERNAME”。 否則“域\用戶名”

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

它應該包括計算機或域名 - 因此無需自己構建這些。 然而,這適用於當前登錄的用戶 - 就像建議您在其他地方使用的環境變量一樣。

此外 - 它不能通過簡單地覆蓋環境變量來編輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM