标签: powershell

WPF 和 Powershell 的键盘快捷键

在下面的代码中:

Add-Type -AssemblyName PresentationFramework
$window = New-Object Windows.Window
$commonKeyEvents = {
    [System.Windows.Input.KeyEventArgs] $e = $args[1]
    if ($e.Key -eq 'ESC') { $this.close() }
    if ($e.Key -eq 'Ctrl+Q') { $this.close() }
}
$window.add_PreViewKeyDown($commonKeyEvents)
$window.ShowDialog() | Out-Null
Run Code Online (Sandbox Code Playgroud)

该'Ctrl+Q'部分不起作用。我怎样才能做到这一点?

wpf powershell keyboard-shortcuts

3
推荐指数
1
解决办法
669
查看次数

在 PowerShell 中比较字符串中的日期

$Deldate = "19-06-2018"
$Newdate = "04-06-2018"
Run Code Online (Sandbox Code Playgroud)

我需要检查哪个日期更大。

if ($Deldate -ge $NewDate) {
    write-host "NewDate is bigger"
} 
else {
    write-host "Deldate is bigger"
}
Run Code Online (Sandbox Code Playgroud)

这对我不起作用,而且格式看起来不是“System.DateTime”。我从外部 CSV 文件获取日期值。我该如何找到解决方案?

powershell

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

如何在 Pester 中比较两个数组与自定义对象

我们正在努力让 Pester 测试失败或通过,具体objects取决于array.

测试.ps1

#require Assert
#require Pester

$Expected = @(
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)
$Actual = @(
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
    [PSCustomObject]@{Name1 = 'Text1';Name2 = 'Text2'}
)

Describe 'comparing arrays' {
    Context 'Assert-Equivalent' {
        it 'should be green' {           
            Assert-Equivalent -Actual $Expected -Expected $Expected
        }
        it 'should be green' {        
            Assert-Equivalent -Actual $Actual -Expected $Expected
        }
        it 'should be red' {        
            $Wrong = @(
                [PSCustomObject]@{Name1 = 'Text1';Name2 …
Run Code Online (Sandbox Code Playgroud)

arrays powershell compare pester

3
推荐指数
1
解决办法
2528
查看次数

存储自动电子邮件脚本的帐户凭据(尤其是密码)的最佳方法是什么?

我正在编写一个简单的脚本(Windows powershell)来发送自动电子邮件。计算机将始终运行收集数据,然后定期发送电子邮件。发送电子邮件的一部分显然是收集凭据,由于它是自动化的,我们不能每次都让有人在那里输入用户和密码。显而易见的解决方案是将信息存储在脚本中的 $user 和 $pass 变量中,但这对我来说似乎非常不安全,并且容易受到攻击。有没有更好的方法来做到这一点?也许使用用户地址设置一次安全电子邮件连接,而不必再次输入?我是 powershell 新手,所以我不太清楚执行此操作的最佳方法。目前我正在做的是:

$from = 'UserEmail@anotherEmail.com'
$to = 'someEmail@SomeMail.com'
$smtpServer = 'smtp-mail.outlook.com'
$smtpPort = '587'
$mailSubject = 'PowerShell Script Email Test'
$password = 'p@ssword'
$mailBody = 'body text'
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $from, $($password | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -To "$to" -From "$from" -Subject $mailSubject -SmtpServer $smtpServer -UseSsl -Credential $credentials -BodyAsHtml -Body $mailBody
Run Code Online (Sandbox Code Playgroud)

任何建议或阅读文档将不胜感激

security email powershell automation email-verification

3
推荐指数
1
解决办法
3096
查看次数

不能使用非字符串的 TFS/VSTS 自定义变量

我一直在使用 TFS 从 PowerShell 创建一些发布变量。然后,我可以在 TFS 中的后续任务中使用“$Env:Server”来引用此输出,这看起来很棒!例如:

任务 1 返回服务器名称,然后创建 TFS 变量:

Write-Host "##vso[task.setvariable variable=Server]"MySevrer
Run Code Online (Sandbox Code Playgroud)

任务 2 使用此信息:

Write-Output $env:Server
MyServer
Run Code Online (Sandbox Code Playgroud)

当以其他格式(如数组或哈希表)输出某些内容时,这不起作用。创建的变量只是字符串,正如文档中使用“Write-Host”所述的那样。

任务1返回:

##vso[task.setvariable variable=Server] System.Collections.DictionaryEntry
Run Code Online (Sandbox Code Playgroud)

任务2不能使用这个:

Write-Output $env:Server    
System.Collections.DictionaryEntry
Run Code Online (Sandbox Code Playgroud)

创建的输出

我尝试将其输出为数组格式的字符串,例如:

[String]$Server = '@{MyServer=@("192.168.0.1")}'
Write-Host "##vso[task.setvariable variable=Server]"$Server
Run Code Online (Sandbox Code Playgroud)

当我提到这一点时,我尝试在 PowerShell 中将其转换回数组,但是,我在脚本中执行此操作时遇到了问题,因为它将将该字符串视为单个数组对象。(不是有值的数组)

有谁知道是否可以根据 Powershell 任务输出的信息来解析 Team Service 的任务/任务任务组之间的哈希表或数组?

我目前已经通过编写包装器/编排函数来解决这个问题,但这对我们来说不是理想的方法。我们目前的版本是 15.117.26714.0,但我在新版本中看不到任何内容。

variables powershell tfs task azure-devops

3
推荐指数
1
解决办法
6819
查看次数

powershell 中的 System.Windows.Forms.ListView 隐藏输出

用下面的代码

$listView = New-Object System.Windows.Forms.ListView
$listView.View = 'Details'
$listView.Width = 300
$listView.Height = 300
$listView.Columns.Add('User Name')
Run Code Online (Sandbox Code Playgroud)

DisplayIndex:0 索引:0 ImageIndex:-1 ImageList:ImageKey:ListView:System.Windows.Forms.ListView,Items.Count:0 名称:文本:用户名 TextAlign:左标签:宽度:60 站点:容器:

它在 powershell 控制台中输出以上内容。我怎样才能隐藏这个输出。

它干扰我在 -noconsole 模式下使用此脚本进行转换https://gallery.technet.microsoft.com/scriptcenter/PS2EXE-GUI-Convert-e7cb69d5

powershell listview winforms

3
推荐指数
1
解决办法
1079
查看次数

从 Powershell 脚本中提取 AST

给定一个 powershell 脚本(例如 $C=$a+$b; $d = $e*$f),我尝试访问脚本 AST 中的每个节点。

\n\n

目前,我\xe2\x80\x99已经尝试过:

\n\n
$code = {$C=$a+$b; $d = $e*$f}\n$astVisitor = [System.Management.Automation.Language.AstVisitor]\n$visit = $code.Ast.Visit($astVisitor)\n
Run Code Online (Sandbox Code Playgroud)\n\n

but I\xe2\x80\x99m running into the following error: Cannot find an overload for "Visit" and the argument count: "1".

\n\n

What\'s the right way to access the ast data structure and use the visit method correctly to loop through every node in the tree? I didn\'t find the documentation of the ast api very helpful.

\n\n

Thanks very …

powershell abstract-syntax-tree

3
推荐指数
1
解决办法
1094
查看次数

sqlpackage表列表

我正在尝试使用动态表列表从 powershell 运行 sqlpackage,但将表列表放入 var 时出现错误。我需要将表列表放入 var 中,因为它将动态生成。

下面的代码工作得很好:

.\sqlpackage.exe `
    /a:Export `
    /scs:"Data Source=$($ServerName);Initial Catalog=$($SourceDbName);User ID=my_user;Password=my_password" `
    /tf:$BacPacFileName `
    /p:TableData=[dbo].[QRTZ_CALENDARS] /p:TableData=[dbo].[QRTZ_SCHEDULER_STATE]
Run Code Online (Sandbox Code Playgroud)

但是,当我将表列表放入 var 中时,它失败了:

$tableList = "/p:TableData=[dbo].[QRTZ_CALENDARS] /p:TableData=[dbo].[QRTZ_SCHEDULER_STATE]"

.\sqlpackage.exe `
    /a:Export `
    /scs:"Data Source=$($ServerName);Initial Catalog=$($SourceDbName);User ID=my_user;Password=my_password" `
    /tf:$BacPacFileName `
    $($tableList)
Run Code Online (Sandbox Code Playgroud)

错误文本:

.\sqlpackage.exe:参数“TableData”具有无效值:
'[dbo].[QRTZ_CALENDARS] /p:TableData=[dbo].[QRTZ_SCHEDULER_STATE]'。
必须使用两部分表指定从中提取数据的表
名称格式:schema_name.table_identifier。
行:1 字符:1
+ .\sqlpackage.exe `
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (参数 'T...ble_identifier.:String) [], RemoteException
    + FullQualifiedErrorId : NativeCommandError / 附近的语法不正确。

sql-server powershell

3
推荐指数
1
解决办法
1626
查看次数

PowerShell 多线程压缩目录

我有一个 rootDir,下面有 3 个目录,我只想压缩每个目录并使用多线程代码监听结果,所以我尝试了以下方法:

Set-Location "C:\test"
$sw = [Diagnostics.Stopwatch]::StartNew()
Get-Job | Remove-Job
$rootDirectory = $PWD
$dirs = $(Get-ChildItem -Path $rootDirectory -Directory).Name
# $dirs = d1, d2, d3
$sb = {
    Param($init)
    Compress-Archive -Path $init -DestinationPath "$init.zip" -CompressionLevel NoCompression
}

$jobs = @()
$dirs | ForEach-Object {
    $jobs += Start-Job -ScriptBlock $sb -ArgumentList $_
}

Wait-Job -Job $jobs | Out-Null
Receive-Job -Job $jobs
Run Code Online (Sandbox Code Playgroud)

我有

路径“d1”不存在或者不是有效的文件系统路径。
    + CategoryInfo : InvalidArgument: (d1:String) [压缩存档],InvalidOperationException
    +FullyQualifiedErrorId:ArchiveCmdletPathNotFound,压缩存档
    + PS 计算机名 : 本地主机

如果我连续运行压缩命令,而不使用Start-Job,一切似乎都有效。

powershell multithreading powershell-4.0

3
推荐指数
1
解决办法
815
查看次数

使用 ValueFromPipelineByPropertyName 从管道获取价值

我在使用从管道获取值时遇到一些问题ValueFromPipelineByPropertyName。

当我运行Get-Input -ComputerName 'PC-01' | Get-Datacmdlet时Get-Input,应仅返回计算机名称“PC-01”,而该Get-Data函数应返回“从 Get-Input 传递的值:PC-01”。相反,我收到此错误:

Get-Data :输入对象不能绑定到命令的任何参数
要么是因为该命令不采用管道输入,要么是输入及其
属性与采用管道输入的任何参数都不匹配。
行:1 字符:33
+ 获取输入-计算机名 PC-01 | 获取数据
+ ~~~~~~~~~
    + CategoryInfo : InvalidArgument: (PC-01:PSObject) [获取数据],ParameterBindingException
    + FullQualifiedErrorId :InputObjectNotBound,获取数据

我构建了这两个小示例 cmdlet,只是为了掌握使用管道的窍门。

function Get-Input {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [string]$ComputerName
    )

    Process {
        Write-Output -InputObject $ComputerName
    }
}

function Get-Data {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [string]$ComputerName
    )

    Process {
        Write-Output …
Run Code Online (Sandbox Code Playgroud)

powershell pipeline

3
推荐指数
1
解决办法
5275
查看次数