gpt4 book ai didi

Powershell获取当前域名或计算机名\用户以及两者之间的区别

转载 作者:行者123 更新时间:2023-12-03 08:51:49 24 4
gpt4 key购买 nike

我正在制作一个脚本,用于将我的 API 注册为 Windows 服务。我遵循了本指南 here因为我是 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

最佳答案

看这里: About Environment Variables

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

关于用户:
是本地用户还是域用户?如果是本地,请使用“计算机名\用户名”。否则“域\用户名”

关于Powershell获取当前域名或计算机名\用户以及两者之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58445871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com