我正在尝试编写一个 Powershell 脚本,该脚本接受他们想要更改的用户名的输入,然后接受他们希望他们的职位和部门更改为什么的输入。
在 Active Directory 中更改职位和部门的语法是什么?
这会按预期创建一个新目录:
new-item "c:\newdir" -itemtype directory
Run Code Online (Sandbox Code Playgroud)
这也有效:
$arr = "one", "two"
$arr | new-item -name {$_} -itemtype directory
Run Code Online (Sandbox Code Playgroud)
产生两个新文件夹:c:\one和c:\two(假设当前目录是 c:)。
这会失败并显示错误“不支持给定路径的格式”。
$arr = "c:\one", "c:\two"
$arr | new-item -name {$_} -itemtype directory
Run Code Online (Sandbox Code Playgroud)
如何使用路径中的驱动器号进行此操作?
我需要一个脚本来启动谷歌浏览器,然后发送一个信号让它进入全屏模式(通常通过按 f11 来完成)。
Chrome 安装到默认位置。
我尝试过的一个例子是:
Start-Process -FilePath "C:\Program Files(x86)\Google\Chrome\Application\chrome"
Run Code Online (Sandbox Code Playgroud)
这个简单的字符串甚至不适合我。
我有很多 powershell 脚本,所以我用几个 cmdlet 创建了一个自定义日志记录 Powershell 模块。问题是我们无法让所有用户下载自定义模块并将其放入他们的 PSModulePath 中。
理想情况下,如果找不到 cmdlet,我希望脚本以静默方式继续运行并且不显示任何错误,但我还没有找到一个好的方法。现在我们收到这样的消息:
C:> foo-bar
foo-bar : The term 'foo-bar' is not recognized as the name of a cmdlet, function, script file, or operable program...
Run Code Online (Sandbox Code Playgroud)
设置 ErrorActionPreference 将抑制消息,但它也会抑制所有其他不需要的错误消息。
我可以在每个脚本中编写一个自定义函数,在调用自定义 cmdlet 之前检查模块是否正在加载,但似乎应该有更好的方法来做到这一点。
我对如何在强制变量保持为空的情况下进行错误处理感到困惑。
function parse-com{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[string[]]$list
)
...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果没有为 $list 传递参数,则会提示我输入它,但如果我只是按回车键(将空值传递给 $list),则会抛出错误。我宁愿做的是抛出一个使用声明和/或优雅地退出。例子...
PS C:\Users\memyself> parse-com
cmdlet parse-com at command pipeline position 1
Supply values for the following parameters:
list[0]:
parse-com : Cannot bind argument to parameter 'list' because it is an empty array.
At line:1 char:1
+ parse-com
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [parse-com], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,parse-com
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 PowerShell 替换多个文本文件的某些列中的字符。我让它工作得很好,只是我需要忽略每个文件中的第一行和最后一行,我无法让它工作。
这是我到目前为止:
$Location = "C:\Users\gerhardl\Documents\Tenacity\TEMP\POWERSHELL TESTS"
$Data = "$Location\*.TXT"
$Output = "$Location\Fixed"
Get-Item $Data |
ForEach-Object {
$file = $_
$_ |
Get-Content |
ForEach-Object {
$Beginning = $_.Substring(0,105)
$Account = $_.Substring(105,20) -replace "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]", " "
$End = $_.Substring(125)
'{0}{1}{2}' -f $Beginning,$Account,$End
} |
Set-Content -Path (Join-Path $Output $file.Name)
}
Run Code Online (Sandbox Code Playgroud)
我知道有类似的线程,但似乎我的 For Each 循环不能很好地处理这些建议。
我需要修改我的 Azure Web 应用程序文件的内容,例如 Web.config 和文本文件。使用 Kudu 命令行 API,我可以使用以下内容创建目录或处理对象:
$username = "`$myuser"
$password = "mypass"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$apiUrl = "https://mywebapp.scm.azurewebsites.net/api/command"
$commandBody = @{
command = "md D:\home\site\wwwroot\newDirectory"
}
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $commandBody) | Out-Null
Run Code Online (Sandbox Code Playgroud)
如何通过 Kudu Command API 修改文件?我的理想状态是使用以下内容通过命令行 API 执行 PowerShell:
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File myFile.txt"
Run Code Online (Sandbox Code Playgroud)
当我在 Kudu 界面的 CMD 调试控制台中输入此命令时,上述命令有效,但我需要通过 API 调用它。我尝试了以下方法:
$username = "`$myuser"
$password = "mypass"
$base64AuthInfo …Run Code Online (Sandbox Code Playgroud) Function Check-PC
{
$PC = Read-Host "PC Name"
If($PC -eq "exit"){EXIT}
Else{
Write-Host "Pinging $PC to confirm status..."
PING -n 1 $PC
}
Run Code Online (Sandbox Code Playgroud)
这是我写到 PowerShell 脚本中的一个函数片段。我希望该函数在 PowerShell 的新实例中运行,而不是在主窗口中运行。
是否可以在 PowerShell 的单独进程中运行它而不将其编写为单独的脚本并调用脚本?像这样的东西:
$x= Start-Process powershell | Check-PC
Run Code Online (Sandbox Code Playgroud)
如果可能,我需要将所有内容都保存在一个脚本中。
我们正在标记我们的 AWS 实例,我想通过 TAG 引用检索我们所有实例(ELB、S3、EC2、安全组)的列表。例如,我们始终使用以下内容标记我们的资源:{ "Key": "Project", "Value": "bananas" },
我们如何通过 power-shell 获取包含 TAG Project 值“bananas”的所有资源的列表?
我能够使用以下脚本获取我所有的 EC2:
$instance = Get-EC2Instance
-Filter @( @{name='tag:Project'; values="bananas"};
@{name='instance-state-code'; values = 16} )
| Select-Object -ExpandProperty instances #Get instance ID 忽略任何终止的实例 $instance | 导出-CSV "C:\ec2.csv"
但我不确定如何使用一个脚本获取我所有的标记资源。
powershell amazon-web-services aws-powershell aws-resource-group
我有一种情况,我必须在我的 powershell 脚本的 try 块中抛出多个自定义异常,如下所示
try {
if (!$condition1) {
throw [MyCustomException1] "Error1"
}
if (!$condition2) {
throw [MyCustomException2] "Error2"
}
}catch [MyCustomException1] {
#do some business logic
}catch [MyCustomException2] {
#do some other business logic
}catch{
#do something else
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不编写 .net 类MyCustomException1和MyCustomException2. 我不必在类中存储任何信息,但我只需要一种区分异常的方法。我可以做如下,但我只是想知道是否有更清洁的东西。
try {
if (!$condition1) {
throw "Error1"
}
if (!$condition2) {
throw "Error2"
}
}catch {
if($_.tostring() -eq "Error1"){
Write-Host "first exception"
}elseif($_.tostring() -eq "Error2"){
Write-Host "Second exception"
}else {
Write-Host …Run Code Online (Sandbox Code Playgroud)