Powershell PS1文件作为系统运行时出现问题

Powershell PS1文件作为系统运行时出现问题,powershell,powershell-5.0,Powershell,Powershell 5.0,我试图执行一个使用新的PS 5.1 cmdlet Remove LocalUser的PS1文件,但遇到了一些问题。下面是我正在运行的代码块,它将检查Administrator组中的所有本地帐户,如果它们不在$IgnoreList中,则将其删除: $ErrorActionPreference="SilentlyContinue" Stop-Transcript | out-null $ErrorActionPreference = "Continue" Start-Transcript -path

我试图执行一个使用新的PS 5.1 cmdlet Remove LocalUser的PS1文件,但遇到了一些问题。下面是我正在运行的代码块,它将检查Administrator组中的所有本地帐户,如果它们不在$IgnoreList中,则将其删除:

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\BigFix\AdminAccounts\output.txt -append

$users = Get-WmiObject win32_groupuser | Where-Object { $_.GroupComponent -match ‘administrators’ } | ForEach-Object {[wmi]$_.PartComponent }
$IgnoreList = "SOMEACCOUNTNAME"

#Write-host $users.name


:OuterLoop
foreach ($user in $users) {
    foreach ($account in $IgnoreList) {
        if ($user.name -like "$account") {
            continue OuterLoop
        }
    }

    Remove-LocalUser -Name $user.Name
    }
Stop-Transcript
如果我登录到本地管理员帐户,则此脚本在目标计算机上运行良好,但在通过端点管理控制台部署时不会运行,因为它作为系统帐户运行

该机器正在运行以下版本的PS:

Name                           Value                                                                                                                                 
----                           -----                                                                                                                                 
PSVersion                      5.1.14409.1005                                                                                                                        
PSEdition                      Desktop                                                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                               
BuildVersion                   10.0.14409.1005                                                                                                                       
CLRVersion                     4.0.30319.36373                                                                                                                       
WSManStackVersion              3.0                                                                                                                                   
PSRemotingProtocolVersion      2.3                                                                                                                                   
SerializationVersion           1.1.0.1
以下是由系统帐户运行时的成绩单文件输出:

**********************
Windows PowerShell transcript start
Start time: 20170213172829
Username: **DOMAIN**\SYSTEM
RunAs User: **DOMAIN**\SYSTEM
Machine: QA-G2-CUCNOV-V4 (Microsoft Windows NT 6.1.7601 Service Pack 1)
Host Application: powershell.exe -executionpolicy unrestricted -NonInteractive -NoProfile -file c:\BigFix\AdminAccounts\removeAdmins.ps1
Process ID: 3244
PSVersion: 5.1.14409.1005
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1005
BuildVersion: 10.0.14409.1005
CLRVersion: 4.0.30319.36373
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\BigFix\AdminAccounts\output.txt
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

**********************
Windows PowerShell transcript end
End time: 20170213172829
**********************

任何关于为什么会发生这种情况的想法都将不胜感激

我没有powershell 5.0来验证这一点,但您可以在脚本开始时编写行以导入Microsoft.powershell.LocalAccounts模块,然后重试。我添加了import module命令,现在也导致了一个错误:
import module:指定的模块“Microsoft.powershell.LocalAccounts”未加载,因为没有在任何模块目录中都找到了有效的模块文件当命令在Shell本地运行时,它将加载模块fine运行此脚本的机器的操作系统是什么?Windows 7 Professional和POS Ready 7。两者都给出了相同的错误。以下可能是原因。注意PrincipalSource属性是描述对象源的LocalUser、LocalGroup和LocalPrincipal对象的属性。PrincipalSource仅受Windows 10、Windows Server 2016和更高版本的Windows操作系统支持。对于早期版本,属性为空。支票条款