出于某种原因,当 ForEach 别名工作正常时,当使用 ( $Thing in $Things) 语法时, ForEach-Object 的 % 别名会引发异常。
这里有两个例子:
使用 % 别名:
$ints = @(1, 2, 3, 4, 5)
% ($i in $ints)
{Write-Host $i}
Run Code Online (Sandbox Code Playgroud)
这将失败并在表达式或语句中出现错误 Unexpected token 'in'。
使用 ForEach 别名:
$ints = @(1, 2, 3, 4, 5)
foreach ($i in $ints)
{Write-Host $i}
Run Code Online (Sandbox Code Playgroud)
这毫无问题地成功。
如果它们都是 ForEach-Object 的别名,为什么会有区别?
我可以像这样在 powershell 中获得登录用户的权限:
whoami /priv
Run Code Online (Sandbox Code Playgroud)
如何为我未登录的用户执行此操作?
我们正在使用TfsDeployer和 PowerShell 脚本来删除Remove-Item
在部署新版本之前使用的文件夹。有时 PS 脚本会失败并显示错误:
删除项目:无法删除项目 Services\bin:该进程无法访问文件 Services\bin',因为它正被另一个进程使用 Get-ChildItem -Path $Destination -Recurse | Remove-Item <<<< -force -recurse + CategoryInfo : WriteError: (C:\Program File..\Services\bin:DirectoryInfo) [Remove-Item], IOExceptionfullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
我试图按照答案(来自:在 PowerShell 中强制删除文件和目录有时会失败,但并非总是如此)通过管道Get-ChildItem -Recurse进入Remove-Item.
Get-ChildItem * -Include *.csv -Recurse | Remove-Item
Run Code Online (Sandbox Code Playgroud)
但是错误仍然会定期发生。我们正在使用解锁器手动终止锁定应用程序,(通常是 w3wp),但我更喜欢找到自动化的解决方案。
另一个(不理想的)选项是抑制 powershell-errors
Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议。
我已经尝试重新安装 SQL Server 2014,但在尝试重新安装时仍然收到以下错误消息import-module sqlps。存在相同的注册表项,但附加了 120。我可以使用像Invoke-SqlCmd. 不确定这里是什么问题。任何帮助表示赞赏。
在 x86 窗口中:
PS H:> import-module sqlps -disablenamechecking
Get-ItemProperty : Cannot find path 'HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps' because it does not exist.
At C:\Users\russell_johnson\Documents\WindowsPowerShell\Modules\sqlps\sqlps.psm1:26 char:13
+ $item = Get-ItemProperty $sqlpsreg
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKLM:\SOFTWARE...owerShell.sqlps:String) [Get-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
Run Code Online (Sandbox Code Playgroud)
在 x64 窗口中:
PS H:\> import-module sqlps
Get-ItemProperty : Cannot find path 'HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps' because it does …Run Code Online (Sandbox Code Playgroud) 我编写了一个快速脚本来比较跨服务器的文件差异,然后提示是否镜像更改。如果没有任何更改,我希望它智能地不提示复制。
剧本:
robocopy "$cSource" "$cDestination" /E /L /FP
$cDecision = Read-host "Continue = 1, anything else = Stop"
if ($cDecision -eq "1") {
robocopy "$cSource" "$cDestination" /MIR /FFT /Z /W:5 /MT:64 /XX /log:$cLogLocation
Invoke-item $cLogLocation
}
else { Write-host "Quit!" }
Run Code Online (Sandbox Code Playgroud)
如果 Robocopy 报告“不匹配”、“失败”、“额外”和“复制”为 0,我希望 Powershell 不会要求继续。对于这个特定的项目来说,这不是很重要,但我可以想到这种检查的几个有用的用途。谢谢!
我是一名Citrix管理员,希望限制我们服务器上的一般用户使用 PowerShell 运行他们自己的脚本,或以交互方式使用它。我们已经禁止通过GPO使用命令提示符,但是在 PowerShell 可用的情况下,这基本上没用。
我发现 Powershell.exe 有一个命令行选项,-NoInteractive允许用户运行脚本,但它没有为他们提供交互式命令提示符。问题是我还没有找到强制powershell以这种方式运行的方法。我什至创建了一个 C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 启动脚本来检查-NoInteractive参数,但用户可以通过简单地启动带有-NoProfile参数的Powershell.exe 来绕过它.
另一个问题是我们确实使用了很多 PowerShell 脚本来为用户启动应用程序,并且登录脚本的一部分是用 PowerShell 编写的,需要在用户上下文中运行,所以我不能简单地对 EXE 文件进行 ACL。我需要他们能够使用 PowerShell,只是不能以交互方式使用。最终,我们希望强制AllSigned执行执行策略并签署所有脚本,以便用户可以运行的唯一内容是我们(管理员)创建和/或签署的脚本。
我试过在谷歌上搜索这个答案,发现很多人都在使用-NoInteractive,但我还没有找到有人试图强迫它的实例。有任何想法吗?
这让我发疯!
显然,使用 servermanager 模块应该和输入一样简单:
Import-Module servermanager
Run Code Online (Sandbox Code Playgroud)
问题是我没有。
PS C:\Users\Administrator> Get-Module -ListAvailable
Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules
ModuleType Name ExportedCommands
---------- ---- ----------------
Manifest BitsTransfer {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfe...
Manifest CimCmdlets {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstanc...
Script ISE {New-IseSnippet, Import-IseSnippet, Get-IseSnippet}
Manifest Microsoft.PowerShell.Diagnostics {Get-WinEvent, Get-Counter, Import-Counter, Export-Count...
Manifest Microsoft.PowerShell.Host {Start-Transcript, Stop-Transcript}
Manifest Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-ItemProperty, Join-Pa...
Manifest Microsoft.PowerShell.Security {Get-Acl, Set-Acl, Get-PfxCertificate, Get-Credential...}
Manifest Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide...}
Manifest Microsoft.WSMan.Management {Disable-WSManCredSSP, Enable-WSManCredSSP, Get-WSManCre...
Script PSDiagnostics {Disable-PSTrace, Disable-PSWSManCombinedTrace, Disable-...
Binary PSScheduledJob {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-...
Manifest TroubleshootingPack …Run Code Online (Sandbox Code Playgroud) 我需要在 Windows Server 2012 上安装 AD PowerShell 模块,但我无法承受服务器在工作时间重启。
我的问题是可以通过添加/删除 Windows 功能安装 AD 模块而不需要重新启动吗?
如果是这样,PowerShell 需要(最少)哪些 AD 模块才能运行此命令
Search-ADAccount -LockedOut
Run Code Online (Sandbox Code Playgroud) 我必须通过 PowerShell 检索代理服务器地址和端口,以便我可以使用该Invoke-Webrequest -uri http://some-site.com -Proxy命令。预期的输出应该类似于http://proxy-server.com:port。
我有一个 PowerShell 函数来检索代理服务器地址和端口,所以我们可以在脚本中使用它?
我编写了一个脚本,目的是快速管理WSUS进程,我有一些硬编码的东西,但更愿意使用 PowerShell。特别是 Approve-WsusUpdate 的“目标”组。
目前我正在做这样的事情:
#Select Target Group for Update Approval:
$TargetComputerGroups = "All Computers", "Unassigned Computers", "Clients", "Servers", "Test", "View Templates"
$UserPrompt = @"
Please select a Computer Group from the below options:
1) All Computers (Selects all of the below)
2) Unassigned Computers
3) Clients
4) Servers
5) Test
6) View Templates
Enter selection
"@
###Record user selection to varirable
$TargetComputerGroupTemp = Read-Host -Prompt $UserPrompt
###Convert their choice to the correct 0-index array value.
$TargetComputerIndex = …Run Code Online (Sandbox Code Playgroud) powershell ×10
windows ×2
deployment ×1
mirror ×1
proxy ×1
robocopy ×1
scripting ×1
sql-server ×1
wsus ×1