Powershell获取当前DOMAIN OR COMPUTER NAME\USER以及两者的区别
Powershell get current DOMAIN OR COMPUTER NAME\USER and the difference between the two
我正在制作一个脚本,用于将我的 API 注册为 windows 服务。由于我是 PS:
的新手,因此我遵循了本指南 here 并尽我所能填写了它
$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 上 运行。
我也不知道我应该使用域还是计算机 name\user?在什么情况下我需要其中之一?
编辑: 在@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
看看这里:
About Environment Variables
$env:COMPUTERNAME
$env:USERNAME
$env:USERDNSDOMAIN
关于用户:
是本地用户还是域用户?如果是本地,请使用 'COMPUTERNAME\USERNAME'。否则 'DOMAIN\USERNAME'
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
它应该包括计算机或域名 - 所以不需要自己构建这些。
然而,这是针对当前登录的用户的 - 就像建议您在其他地方使用的环境变量一样。
此外 - 它不能通过简单地覆盖环境变量来编辑。
我正在制作一个脚本,用于将我的 API 注册为 windows 服务。由于我是 PS:
的新手,因此我遵循了本指南 here 并尽我所能填写了它$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 上 运行。
我也不知道我应该使用域还是计算机 name\user?在什么情况下我需要其中之一?
编辑: 在@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
看看这里: About Environment Variables
$env:COMPUTERNAME
$env:USERNAME
$env:USERDNSDOMAIN
关于用户:
是本地用户还是域用户?如果是本地,请使用 'COMPUTERNAME\USERNAME'。否则 'DOMAIN\USERNAME'
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
它应该包括计算机或域名 - 所以不需要自己构建这些。 然而,这是针对当前登录的用户的 - 就像建议您在其他地方使用的环境变量一样。
此外 - 它不能通过简单地覆盖环境变量来编辑。