简体   繁体   English

SCCM Powershell脚本中的$ ENV:USERNAME

[英]$ENV:USERNAME in SCCM Powershell script

Ok, I saw a answer here about this very same question but I'm not understanding it and what is the next step. 好的,我在这里看到了关于同一问题的答案,但我不理解它,下一步是什么。 The original question What am I supposed to do if I want this line to work 最初的问题 ,如果我想让这条线工作,我应该怎么办

$from = "$dirFiles\config.xml"
$to = "C:\Users\$env:USERNAME\AppData\Roaming\Folder\Folder\config.xml"
Copy-Item $from $to -recurse

since $env:username resolves into my computer name when deployed though SCCM. 因为通过SCCM部署时$ env:username解析为我的计算机名。 How and where am I supposed enter: 我应该在哪里输入?

([Security.Principal.WindowsIdentity]::GetCurrent()).Name.replace("$ENV:USERDOMAIN\","")

Hope you understand me 希望你能理解我

edit** I found what I could use thanks to bluuf and Syberdoor who pointed me in the right direction 编辑**由于bluuf和Syberdoor向我指出了正确的方向,我找到了可以使用的东西

**$CurrentUser = (Get-LoggedOnUser).UserName**
$from = "$dirFiles\config.xml"
$to = "C:\Users\$CurrentUser\AppData\Roaming\Folder\Folder\config.xml"
Copy-Item $from $to -recurse

By default SCCM programs are executed with the SYSTEM account of the current computer. 默认情况下,SCCM程序使用当前计算机的SYSTEM帐户执行。

If this is a program from the package/program model what you have to do to change this is in the properties of the program go to "Environment" select "program can run: Only when a user is logged on" and "Run with user's rights" possibly also go to "Advanced" and select "When this program is assigned to a computer: Run once for every user who logs on" 如果这是程序包/程序模型中的程序,则必须更改此程序,方法是在程序的属性中转到“环境”,然后选择“程序可以运行:仅当用户登录时才能运行”和“使用用户的身份运行”。权限”可能还转到“高级”,然后选择“将程序分配给计算机时:对每个登录的用户运行一次”

If it's an application type you have to go to the properties for the Deployment Type and to "User Experience" and there change "Installation Behavior" to "Install for User". 如果是应用程序类型,则必须转到“部署类型”的属性,然后转到“用户体验”,然后将“安装行为”更改为“为用户安装”。

This would be the SCCM internal method to do what you want. 这将是SCCM内部方法来执行您想要的操作。 It of course also means you lose all admin rights and accesses as the context is now the logged on user's. 当然,这也意味着您将丢失所有管理员权限和访问权限,因为上下文现在已成为登录用户的权限。 Access to the userprofile should be no problem (the better environment variable would be $env:appdata btw) but you will also need readaccess to $dirFiles for every user. 对用户配置文件的访问应该没有问题(更好的环境变量为$ env:appdata btw),但是您还需要为每个用户对$ dirFiles进行读访问。

A different approach (if this has only to be done once for all the computers) would be keeping the admin rights and instead of using the environment variable get all users with something like "gci C:\\users" (minus public profile) and then with the admin replace all users files at once. 另一种方法(如果只需要对所有计算机执行一次)将保留管理员权限,而不是使用环境变量,而是使用“ gci C:\\ users”(减去公共配置文件)之类的方式让所有用户,然后用管理员一次替换所有用户文件。

I'm trying to answer the original question you asked: You can create your own variable to replace the $env:username. 我正在尝试回答您提出的原始问题:您可以创建自己的变量来替换$ env:username。 For example below I use $uu: 例如,下面我使用$ uu:

$from = "$dirFiles\config.xml"
$uu = (([Security.Principal.WindowsIdentity]::GetCurrent().Name) -split "\\")[1]
$to = "C:\Users\$uu\AppData\Roaming\Folder\Folder\config.xml"
Copy-Item $from $to -recurse  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM