标签: powershell

如何获取 64 位的 pshome 路径?

32 位的主文件夹路径可使用$PSHome变量 ( c:\Windows\System32\WindowsPowerShell\v1.0\) 访问。

如何访问包含 64 位版本 ( ) 路径的变量c:\Windows\SysWOW64\WindowsPowerShell\v1.0\?

编辑

正如 Jeff Zeitlin 在标记为正确的答案中解释的那样,这个问题是无效的......

powershell

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

如何在 *n* 个测试失败后停止 MsTest 测试执行

我想通过 MS Test(从 Windows 控制台)运行单元测试,只要失败的测试计数超过特定阈值,我就可以停止/终止测试执行。

对于我的用例,当一定比例的测试已经失败时,没有必要继续运行测试。

我只能考虑创建一个新的控制台应用程序来包装 mstest.exe 执行,这样我就可以实时解析标准输出,并最终终止该进程,例如:

var pi = new ProcessStartInfo()
{
    FileName = MS_TEST,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    Arguments = MS_TEST_ARGS
};
int passed = 0; int failed = 0;
using (var process = Process.Start(pi))
{
    while (!process.StandardOutput.EndOfStream)
    {
        string line = process.StandardOutput.ReadLine();
        if (line.Contains("Passed"))
            passed++;
        if (line.Contains("Failed"))
            failed++;
        if (failed >= THRESHOLD)
        {
            process.Kill();
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

谁能提出更好的方法来做到这一点?我认为 MsTest 本身并不支持这一点。

PowerShell 似乎是一个选项,但标准输出重定向并不是微不足道的。

更新

请注意,我无法修改测试代码,我需要在不以任何方式修改测试代码的情况下完成此操作。

powershell unit-testing mstest

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

Powershell 控制台在 Register-ObjectEvent 操作中执行函数时出现问题

在 powershell ISE 中工作正常,但在控制台中我得到:

术语“MainAction”不被识别为 cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

怎么修?

function MainAction () {
    $test = "123"
    Write-Host $test
}

MainAction

$action = {
    try {
        Write-Host in action
        MainAction
    } catch {
        Write-Host $Error    
        $timer.Stop()
        Unregister-Event thetimer
    }
}

$timer = New-Object Timers.Timer    
Register-ObjectEvent -InputObject $timer -EventName elapsed `
                     -SourceIdentifier thetimer -Action $action -OutVariable out
$timer.Interval = 5000
$timer.AutoReset = $true
$timer.Start()
Run Code Online (Sandbox Code Playgroud)

编辑:

我发现我可以使用配置文件来存储函数定义。要创建配置文件,请使用:

New-Item -path $profile -itemType file -force
Run Code Online (Sandbox Code Playgroud)

但我仍然感兴趣为什么 powershell ISE 不需要配置文件来存储和使用Register-ObjectEvent.

powershell

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

检查.ISO是否已安装在powershell中,如果没有则安装

我有一个从旧游戏光盘上复制的 ISO 文件。但为了让我玩游戏,我必须安装 ISO。我编写了一个小批处理文件,该文件运行.ps1PowerShell 文件来挂载 ISO,然后在挂载后运行 EXE 来启动游戏。我的问题是,如果我多次运行该脚本,它将再次挂载 ISO。

我想检查 ISO 是否已附加,如果没有附加则安装它,如果附加则运行 EXE。

这是我必须安装 ISO 的内容:
批量。

ECHO "Mounting Stunt Track Driver"

@ECHO off

Powershell.exe -executionpolicy remotesigned 
-File  "C:\Users\Allen\Documents\Games\Hot Wheels Stunt Track 
Driver\setup\hot98\mount.ps1"

start /d "C:\Users\Allen\Documents\Games\Hot Wheels Stunt Track 
Driver\setup\hot98" stunt.exe
Run Code Online (Sandbox Code Playgroud)

电源外壳

#mounts the image
Mount-DiskImage -ImagePath "C:\Users\Allen\Documents\Games\Hot Wheels Stunt 
Track Driver\setup\hot98\HotwheelsStuntTrack.iso"
Run Code Online (Sandbox Code Playgroud)

powershell batch-file

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

Powershell 和 UTF-8

我有一个用atom创建的html文件test.html,其中包含:

\n\n
\n

测试\xc3\xa9编码utf-8

\n
\n\n

当我使用 Powershell 控制台阅读它时(我使用的是法语 Windows)

\n\n
Get-Content -Raw test.html\n
Run Code Online (Sandbox Code Playgroud)\n\n

我得到这个:

\n\n
Test\xc3\x83\xc2\xa9 encoding utf-8\n
Run Code Online (Sandbox Code Playgroud)\n\n

为什么重音字符打印不正确?

\n

powershell utf-8 utf

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

使用 PowerShell 从 SharePoint 打开 Excel 文档

我正在尝试使用 PowerShell 从 SharePoint 打开 Excel 工作簿。我没有加载 SharePoint 管理单元\xe2\x80\x94我没有它。

\n\n

当 PowerShell 尝试启动工作簿时,SharePoint 会提示输入凭据。问题是我们正在尝试安排脚本,并且我们希望脚本具有类似 SSO 的体验。

\n\n

这是 MWE:

\n\n
$Excel = New-Object -ComObject Excel.Application\n$Workbook = $Excel.Workbooks.Open(http://site/file.xlsx)\n$Worksheet = $Workbook.Sheets.Item($WorksheetName)\n$Excel.Visible = $false\n\n# some Excel manipulation\n\n$Workbook.Close($false)\n$Excel.Quit()\n[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null\nRemove-Variable Excel\n
Run Code Online (Sandbox Code Playgroud)\n

powershell sharepoint sharepoint-2010

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

带有 Autologon.exe 的 Powershell 命令行

有人让“Autologon.exe for Windows v3.10”与 PowerShell v5.1 一起使用吗?

执行1:

以管理员身份运行以下命令:

.\Autologon.exe -n guest10 -d test.com -p Password1 -accepteula yes
Run Code Online (Sandbox Code Playgroud)

错误1:

在此输入图像描述


执行2:

以管理员身份在 powershell 中运行以下命令:

.\Autologon.exe guest10 test.com Password1
Run Code Online (Sandbox Code Playgroud)

错误2:什么也没发生


执行3:

以管理员身份在 powershell 中运行以下命令:

$obj=.\Autologon.exe
        $name ="guest10"
        $domain="test"
        $pass="Password1"
        & $obj $name $domain $pass
Run Code Online (Sandbox Code Playgroud)

错误3:

管道元素中“&”后面的表达式生成了无效的对象。它必须生成命令名称、脚本块或 CommandInfo 对象。

powershell windows-10

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

Jfrog CLI 不尊重配置的 API 密钥

我正在使用 Powershell DSC 设置 Windows Server 2012 R2 盒子。作为配置过程的一部分,我将配置 jfrog.exe 并作为服务帐户执行配置命令:

jfrog rt config --url=$localArtifactoryInstance --user=$username --apikey=$apikey
Run Code Online (Sandbox Code Playgroud)

此命令按预期在服务帐户的主目录 (C:\Users\$serviceAccount.jfrog.jfrog-cli.conf) 中创建 .jfrog.jfrog-cli.conf,其中包含 URL/用户名/apiKey。

配置后,此服务帐户负责将工件从一个存储库“提升”到另一个存储库:

jfrog rt move '$devRepo/(Org)/(Org.Common.Configuration)/Org.Common.Configuration.1.0.0.nupkg' '$prodRepo/{1}/{2}/'
Run Code Online (Sandbox Code Playgroud)

该命令被拒绝并返回 401:

[Error] Artifactory response: 401 Unauthorized
{
  "errors": [
    {
      "status": 401,
      "message": "Bad authentication. Provided username or password are incorrect."
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

但是,当我将jfrog-cli.conf 中已配置的相同 API 密钥添加到命令中时:

jfrog rt move '$devRepo/(Org)/(Org.Common.Configuration)/Org.Common.Configuration.1.0.0.nupkg' '$prodRepo/{1}/{2}/' --ApiKey $apiKey
Run Code Online (Sandbox Code Playgroud)

成功了,我可以通过 Artifactory UI 验证是否成功。

  • 执行该命令的服务帐户与执行 jfrog rt config 的服务帐户相同,并且与 jfrog-cli.conf 中的用户名匹配。
  • API 密钥是在 Artifactory …

powershell artifactory jfrog-cli

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

在 powershell 中打开受密码保护的 Excel

我正在尝试在 powershell 中打开受密码保护的 Excel 工作表,并输出有关工作表中有多少行的报告。

如果工作表没有密码保护,脚本工作得绝对正常,但如果设置了密码,我似乎无法让 powershell 打开它。

我当前的脚本是

$Report = "S:\Business Support\excel tests\MI Tool - Live.csv"
$path = "S:\Business Support\excel tests"
[Array]$Results = $null
$excelSheets = Get-Childitem -Path $path -Include "MI Tool - Live.xlsm" -Recurse
$excel = New-Object -comobject Excel.Application
$excel.visible = $false
$password = "blablabla"
$updatelinks = 0


foreach($excelSheet in $excelSheets)
{
 $workbook = $excel.Workbooks.Open($excelSheet,$updatelinks,$password)
 $rowCount = $null

  $worksheet = $workbook.sheets.item("Data")
  $rowMax = ($worksheet.usedRange.rows).count
  $rowCount += $rowMax

$Results += New-Object Psobject -Property @{
    "File Name"=$excelSheet.Name
    "Row Count"=$rowCount}
$excelSheet.Name …
Run Code Online (Sandbox Code Playgroud)

excel powershell password-protection

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

Windows 中递归删除时出错

我目前正在卸载一些为大量文件生成数据的软件,.jpg并且.nfo我想删除这些文件。我正在尝试使用 PowerShell 来执行此操作,我尝试过的命令是:

del /S *.jpg
Run Code Online (Sandbox Code Playgroud)

并收到以下结果:

删除项目:找不到接受参数“*.jpg”的位置参数。
在行:1个字符:1
+ del /S *.jpg
+ ~~~~~~~~~~~~
+ CategoryInfo:InvalidArgument:(:) [Remove-Item],ParameterBindingException
+ ExcellentQualifiedErrorId:PositionalParameterNotFound,Microsoft.PowerShell .Commands.RemoveItemCommand

谢谢!

windows powershell delete-file

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