【问题标题】:powershell create folder in other userprofile with administrative privilegepowershell 在具有管理权限的其他用户配置文件中创建文件夹
【发布时间】:2017-04-08 00:10:57
【问题描述】:

我有一个具有标准权限的用户,我需要运行一个具有管理员权限的 powershell 脚本来做某事,最后创建一个文件夹并在当前记录的用户配置文件中复制一个文件。 我该怎么做?

示例:

C:\Users\USERNAME\FOO\FOO.TXT

我这样做了,但显然是在我的管理员配置文件中创建文件夹

# DO SOMETHINGS BEFORE   

$directory = $env:USERPROFILE + 'FOO'

    if(!(Test-Path -Path $directory)){
    New-Item -Path $env:USERPROFILE -Name "FOO" -ItemType "directory"
}
Copy-Item "testo.txt" -Destination $directory


# Copy-Item "arDigiCore.ini" -Destination $arDigiSign

提前致谢

编辑:

1 - 我运行我的 powershell 脚本,像标准用户(例如 user1)和管理员(例如 admin1)一样登录。

2 - 脚本安装一个程序,在结束之前,检查并在路径 C:\Users\users1\foo 中创建一个文件夹

注意:我不知道之前登录执行程序的用户名

【问题讨论】:

  • 不清楚你在这里问什么。请逐步描述您想要做什么以及结果应该是什么。
  • 1 - 我运行我的 powershell 脚本,像标准用户(例如 user1)和管理员(例如 admin1)一样登录。 2 - 脚本安装一个程序,在结束之前,检查并在路径 C:\Users\users1\foo 中创建一个文件夹注意:我不知道之前登录执行程序的用户名跨度>
  • 你在这个需要管理权限的脚本中做什么?你希望普通用户运行这个脚本的效果是什么?目前还不清楚您要完成什么。
  • 我使用了命令:$userDir=$CurrentConsoleUserSession.UserName
  • 所以你是远程登录的,你想使用本地登录的人的信息并在本地用户配置文件路径中创建一个文件夹?

标签: powershell


【解决方案1】:

您可以使用query.exe 拉取当前用户。然后过滤不是你的活跃用户。

$user = (((& query user) | ? {$_ -like "*active*" -and $_ -notlike "AdminUserName"}).trim()  -Replace '\s+',' ' -Split '\s')[0]
#Credit to Jaap Brasser https://gallery.technet.microsoft.com/scriptcenter/Get-LoggedOnUser-Gathers-7cbe93ea

然后转换为 SID 并匹配从 Win32_UserProfile 返回的 SID

$NTUser = New-Object System.Security.Principal.NTAccount($user)
$SID = ($NTUser.Translate([System.Security.Principal.SecurityIdentifier])).value
$directory = (gcim Win32_UserProfile | ? {$_.sid -eq $SID}).localpath

if(!(Test-Path -Path (Join-Path $directory FOO))){
    New-Item -Path $Directory -Name "FOO" -ItemType "directory"
}
Copy-Item "testo.txt" -Destination (Join-Path $directory FOO)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 2021-09-30
    • 1970-01-01
    • 2022-12-05
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    相关资源
    最近更新 更多