我正在编写一个脚本,将我的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上运行。
我也不知道是否应该使用域名或计算机名\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发布于 2019-10-18 08:12:47
看看这里:关于环境变量
$env:COMPUTERNAME
$env:USERNAME
$env:USERDNSDOMAIN关于用户:
它是本地用户还是域用户?如果是本地的,请使用“COMPUTERNAME\USERNAME”。“域\用户名”
发布于 2019-10-18 10:10:20
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name它应该包括计算机或域名-所以不需要自己构建这些。但是,这是针对当前登录用户的--就像建议您在其他地方使用的环境变量一样。
此外,它不能通过简单地覆盖环境变量来编辑。
https://stackoverflow.com/questions/58445871
复制相似问题