我有一个通过Intune安装应用程序的任务,98%的安装都很好。但我对其中一些有意见。
这个问题是当一个应用程序不能安装或只能由系统帐户运行。
我尝试创建一个本地管理帐户,然后让脚本以该帐户启动另一个脚本,但是这里windows安全踢了-系统帐户不允许运行start - process
我使用PSExec64.exe来启动powershell.exe
下面是安装
的代码$InstallUser = "IntuneInstaller"
$password = -join ((33..126) | Get-Random -Count 32 | ForEach-Object {[char]$_})
$passwordSecure = ConvertTo-SecureString -AsPlainText $password -Force
$null = New-LocalUser "$InstallUser" -Password $passwordSecure -FullName "$InstallUser" -Description "Automated Install Account" -AccountNeverExpires -PasswordNeverExpires
Add-LocalGroupMember -Group "Administrators" -Member "$InstallUser" -ErrorAction SilentlyContinue
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($InstallUser,$passwordSecure)
Start-Process PowerShell.exe -Credential ($Credentials) -WorkingDirectory "c:sysman" -ArgumentList "c:SysManWriteMyNameInTheSand.ps1 -MyName $env:USERNAME -MyLocation c:sysman -MyMessage $password" -Wait -WindowStyle Hidden
Remove-LocalUser -Name "$InstallUser"
如果我以管理员身份运行它可以正常工作-但如果我以系统帐户运行它,我会得到错误:
Start-Process : This command cannot be run due to the error: Access is denied.
At C:SysManRunInstallAsAdminUser.ps1:20 char:1
+ Start-Process Powershell.exe -Credential ($Credentials) -WorkingDirec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
谁有好的建议?这里的故障排除问题是:
- 当您在相同的环境中重新运行它时,它是否总是失败?
关键是您将[char]33
和[char]126
之间的所有字符作为密码,这些字符可能包括可能影响甚至中断PowerShell.exe
命令行接口的命令行的字符,例如单引号('
):
$Message = "te'st"
Start-Process PowerShell.exe -ArgumentList "-NoExit", "-Command Write-Host $Message" -Wait
字符串缺少结束符:'。
+ CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
+ fulllyqualifiederrid: terminatoreexpectedatendofstring
换句话说,您可能需要使用更复杂的密码生成器,如以下问题
使用随机密码创建临时帐户可能是一件聪明的事情,但是将明文密码传递给C:SysManRunInstallAsAdminUser.ps1
脚本是不安全的(有人可能会简单地欺骗该脚本并创建一些其他管理帐户)。
对于ConvertTo-SecureString -AsPlainText $password -Force
语句,不应该使用SecureString,如果您这样做:
⚠️重要
SecureString对象不应该从String构造,因为敏感数据已经受到不可变的String的内存持久性影响。类。构造SecureString的最佳方法对象来自一次一个字符的非托管源,例如控制台。ReadKey方法。
含义:
$password = -join ((65..90) | Get-Random -Count 32 | ForEach-Object {[char]$_})
$passwordSecure = ConvertTo-SecureString -AsPlainText $password -Force
这样做更安全:
$passwordSecure = [SecureString]::New()
(65..90) | Get-Random -Count 32 | ForEach-Object { $passwordSecure.AppendChar([Char]$_) }