标签: powershell

输入带掩码的密码

我有一个从其他一些帖子拼凑起来的简单例子:

[CmdletBinding(
    DefaultParameterSetName = 'Secret'
)]
Param (
    [Parameter(Mandatory=$True)]
    [string]$FileLocation,

    [Parameter(
        Mandatory = $True,
        ParameterSetName = 'Secret'
    )]
    [Security.SecureString]${Type your secret password},
    [Parameter(
        Mandatory = $True,
        ParameterSetName = 'Plain'
    )]
    [string]$Password
)

if ($Password) {
    $SecretPassword = $Password | ConvertTo-SecureString -AsPlainText -Force
} else {
    $SecretPassword = ${Type your secret password}
}

Write-Host $SecretPassword
$BSTR = `
    [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecretPassword)
    $PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    Write-Host $PlainPassword
Run Code Online (Sandbox Code Playgroud)

https://social.technet.microsoft.com/wiki/contents/articles/4546.working-with-passwords-secure-strings-and-credentials-in-windows-powershell.aspx

如何使用 powershell 的 read-host 功能接受外部服务的密码?

运行脚本时,如何屏蔽输入的密码?

所以而不是这个:

PS C:\temp> .\test.ps1 -FileLocation C:\Temp\file.csv  -Password secret
Run Code Online (Sandbox Code Playgroud)

这个

PS …
Run Code Online (Sandbox Code Playgroud)

powershell

2
推荐指数
2
解决办法
7840
查看次数

如何使用 invoke-sqlcmd 访问数据集中表的特定列?

正如该线程中所讨论的,用户 jkdba 建议了一种不同的方法来访问表的列。由于原始线程正在修复一个附带问题,因此我打开了该线程以了解如何使用 invoke-sqlcmd 访问表的特定列。如果有人有不同的方法,请不要犹豫分享。:)

powershell

2
推荐指数
1
解决办法
4934
查看次数

在 Powershell 中更新 VSTS WorkItem 的正确 Invoke-RestMethod 语法是什么 - 构造包含 Windows 路径的 JSON 字符串

尝试使用 Invoke-RestMethod 更新现有 VSTS 工作项时,我不断收到

您必须在请求正文中传递有效的补丁文档。

这是我要经过的

$Body = '[{ "op": "test", "path": "/rev", "value": 1},{ "op": "add", "path": "/fields/System.IterationPath", "value":"' + $caseIterationPath + '"},{ "op": "add", "path": "/fields/System.AreaPath", "value":"' + $caseAreaPath + '"}]'

Invoke-RestMethod -Uri "$rootUri/_apis/wit/workitems/$($case.id)?$apiVersion" -Method PATCH -ContentType application/json-patch+json -Headers @{Authorization= $authorization} -Body $Body
Run Code Online (Sandbox Code Playgroud)

身体扩展到

"[{ "op": "test", "path": "/rev", "value": 1},{ "op": "add", "path": "/fields/System.IterationPath", "value":"Foo\Bar 2016.4\2016.4.02"},{ "op": "add", "path": "/fields/System.AreaPath", "value":"Foo\Apps\Bar Stool\Eating"}]"
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

powershell azure-devops-rest-api

2
推荐指数
1
解决办法
3492
查看次数

使用 AzureAD 模块检索服务主体的应用程序权限

使用AzureAD模块,我可以使用cmdlet检索服务主体的所有委派权限Get-AzureADServicePrincipalOAuth2PermissionGrant。但是,我找不到类似的 cmdlet 来检索此模块中服务主体的应用程序权限。

AzureAD模块是否提供检索服务主体的应用程序权限的方法?

powershell azure azure-active-directory

2
推荐指数
1
解决办法
4915
查看次数

PowerShell,如何在现有浏览器选项卡中打开 url?

我想制作一个powershell脚本,它将:

  1. 运行 Chrome
  2. 打开一个 google.com 网站
  3. 坚持一段时间
  4. 在同一选项卡中从 google.com ===> bing.com 转到。

所以这是代码,对我有用,它运行 Chrome 并在选项卡中打开 google.com 网站:Start-Process "chrome.exe" "www.google.com"

主要问题,我不知道如何在同一个选项卡中打开新链接。我发现这篇文章https://msdn.microsoft.com/en-us/library/aa768360(v=vs.85).aspx,但我是个菜鸟,无法正确使用这些信息。有没有人,谁能帮助我?请?

browser navigation powershell google-chrome

2
推荐指数
1
解决办法
9214
查看次数

Powershell Excel 自动添加工作表

我正在尝试自动化为每月报告类型工作簿在 excel 中为每个客户名称添加工作表(带有数据)的过程

我认为它应该是直接的......但我使用的方法不起作用......它甚至没有进入制表模式......你能帮我弄清楚我做错了什么吗?

以下是我做的功能

function Excelclientstatstemplate ($clients) {
$Exl = New-Object -ComObject "Excel.Application"
$Exl.Visible = $false
$Exl.DisplayAlerts = $false
$WB = $Exl.Workbooks.Open($excelmonthlysummary) 

$clientws = $WB.worksheets | where {$_.name -like "*$clients*"}

#### Check if Clients worksheet exists, if no then make one with client name ###
$sheetcheck = if (($clientws)) {} Else {


$WS = $WB.worksheets.add
$WS.name = "$clients" 
} 
$sheetcheck 

$WB.Save

# Enter stat labels 
$clientws.cells.item(1,1) = "CPU Count" 
$clientws.cells.item(2,1) =  "RAM"
$clientws.cells.item(3,1) =  "Reserved CPU"
$clientws.cells.item(4,1) =  "Reserved …
Run Code Online (Sandbox Code Playgroud)

com excel powershell worksheet

2
推荐指数
1
解决办法
2万
查看次数

将 invoke 命令的输出封装在一个变量中 - PowerShell

我有一个在远程机器上安装远程桌面服务的脚本(来自 DC)。

我现在正处于检查连接代理(服务器)和连接主机(服务器)上是否安装了 RDS 的阶段。

我想使用 invoke-command 因为远程 powershell 会话似乎太复杂了。

这是我的代码:

$res = Invoke-Command -ComputerName "testpc.eil.local" -ScriptBlock {
if((Get-WindowsFeature -Name "Remote-Desktop-Services").Installed -eq 1)
{
   #i need this output (true or false or a string)
}
else
{
     #i need this output (true or false or a string)
}
}


Write-Host $res
Run Code Online (Sandbox Code Playgroud)

但我的问题是,如何将 invoke-command 中脚本块的输出封装在 DC 可以访问的变量中?如果 RDS 已成功安装或未能写入日志文件,我正在尝试写入

我们如何封装函数的输出并将其传递给运行它的机器?

谢谢

variables powershell scripting invoke-command storing-data

2
推荐指数
1
解决办法
1万
查看次数

使用“1”参数调用“SetAccessRule”的异常:::Powershell 错误

我的 Powershell 脚本出现错误,我试图在其中提供一个文件夹“完全控制”。我可以为域/用户帐户提供完全控制权,但我似乎无法让它为 ComputerName\Users 帐户工作。这对于允许安装的其他软件在我们组织中的各种计算机上运行很重要(它们在“安全”选项卡中都有一个 ComputerName\Users 帐户)。我想将完全控制权授予 [ComputerName\Users] 帐户。

下面的屏幕截图显示了我的脚本可以创建的 DomainUsers/Account,在它下面我无法以任何方式引用 ComputerName\Account 来提供 Total Control -- 在本例中,“Users(OHAIBMG00B7KP\Users)”触发错误。

安全标签丝印

此脚本用于为文件夹创建帐户和总控制权限

$directory = "C:\Program Files (x86)\CAREWare"
$domainName = "DHS"
$group = 'Domain Users'
$inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit"
$propagation = [system.security.accesscontrol.PropagationFlags]"None"
$acl = (Get-Item $directory).GetAccessControl("Access")
$user = "{0}\{1}" -f "$domainName", $group
$user.trim()
$access = "FullControl"
$accessType = "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule -ArgumentList @("$user","$access", "$inherit", "$propagation", "$accessType")
$acl.SetAccessRule($accessRule)
set-acl $directory $acl
Run Code Online (Sandbox Code Playgroud)

这是我尝试更改以下三行以引用 ComputerName\Users 时出现的错误。

$directory = "C:\Program Files (x86)\CAREWare"
$domainName = $env:computername
$group = …
Run Code Online (Sandbox Code Playgroud)

powershell

2
推荐指数
1
解决办法
1万
查看次数

在 PowerShell 中重新启动已完成的后台作业

在 PowerShell 5.1 中,我使用以下命令创建了一个后台作业

Start-Job { while($true) { Get-Random; Start-Sleep 5 } } -Name Sleeper
Run Code Online (Sandbox Code Playgroud)

作业创建得很好,它也返回结果Receive-Job。我用Stop-Jobcmdlet停止了它。

我想在同一个 PS 会话中再次重新启动它。

我执行Get-Jobcmdlet,我看到那里的作业处于停止状态。

如何在不重新创建的情况下重新启动它?

我无法在 about_Jobs 中获取此信息。

脚本块只是一个示例脚本。我更想知道是否有办法重新启动已经存在的工作。

powershell jobs

2
推荐指数
1
解决办法
2030
查看次数

用于重新启动计算机的 Powershell 工作流

代码:

workflow Test-RemoteReboot{
    param ([string[]]$serverNames)
    foreach -parallel($server in $serverNames){
        Restart-Computer -PSComputerName $server -Wait -Force
    }
}

Test-RemoteReboot SP,SP2
Run Code Online (Sandbox Code Playgroud)

问题:这是我在 powershell 中构建的相当长的工作流程的一小段摘录。从各方面来看,这应该有效,但我收到以下错误(即使在单独运行此脚本时):

Microsoft.PowerShell.Utility\Write-Error :运行命令停止,因为首选项变量“ErrorActionPreference”或公共参数设置为停止:跳过计算机 SP2。无法通过 WMI 服务检索其 LastBootUpTime,并显示以下错误消息:RPC 服务器不可用。(来自 HRESULT 的异常:0x800706BA)。在 line:433 char:25 + ... Receive-Job -Job $job -Wait -Verbose -Debug -ErrorAction ... + ~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error] , ActionPreferenceStopException +fullyQualifiedErrorId : System.Management.Automation.ActionPreferenceStopException,Microsoft.PowerShell.Commands.WriteErrorCommand + PSComputerName : [localhost]

我查看了 RPC 服务器不可用的问题。防火墙关闭,RPC 服务正在运行,服务器在域中,工作流从域成员计算机运行,作为域管理员。我可以成功地 PSRemote 进入机器并做任何事情。我什至可以这样做:

workflow Test-LocalRebootRemotely{
param ([string[]]$serverNames)
    foreach -parallel($server in $serverNames){
        InlineScript { Restart-Computer -Force } -PSComputerName …
Run Code Online (Sandbox Code Playgroud)

powershell

2
推荐指数
1
解决办法
3143
查看次数