标签: powershell

如何在PowerShell字符串文字中编码Unicode字符代码?

如何在PowerShell字符串中编码Unicode字符U + 0048(H)?

在C#中我会这样做:"\u0048"但是这似乎在PowerShell中不起作用.

unicode powershell string-literals unicode-literals

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

LINQ可以在PowerShell中使用吗?

我试图在PowerShell中使用LINQ.看起来这应该是完全可能的,因为PowerShell是建立在.NET Framework之上的,但我无法让它工作.例如,当我尝试以下(人为的)代码时:

$data = 0..10

[System.Linq.Enumerable]::Where($data, { param($x) $x -gt 5 })
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

无法找到"Where"和参数计数的重载:"2".

不要紧,这可以通过以下方式实现Where-Object.这个问题的关键不是找到在PowerShell中执行此操作的惯用方法.如果我可以使用LINQ,那么PowerShell中的一些任务将更轻松.

linq powershell

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

Powershell操纵主机文件

我正在查看是否可以创建powershell脚本来更新主机文件中的内容.

有人知道是否有任何使用PowerShell或任何其他脚本语言操作主机文件的示例?

谢谢.

powershell

43
推荐指数
6
解决办法
5万
查看次数

无法在Microsoft Powershell中使用`mvn -D`参数运行Maven,但可以在命令提示符下运行

我试图从命令行构建我们的Web项目,但跳过测试.我正在使用该命令mvn clean install -Dmaven.test.skip=true.

当我从传统的黑白命令提示符(也称为DOS shell)运行该命令时,该命令有效,但是当我从"Windows PowerShell"命令运行它时,我收到以下错误:

[ERROR] Unknown lifecycle phase ".test.skip=true". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin- artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepar e-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-site, site, post-site, site-deploy, pre-clean, clean, po st-clean. -> [Help 1]

造成这种差异的原因是什么让PowerShell的行为与传统的命令提示符相似?

这是在Windows 7上运行的.

windows powershell command-line command maven

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

如何调试NuGet包的install.ps1脚本

因此,我们可以在NuGet包中包含安装/卸载powershell脚本.我试过,但我的install.ps1不起作用.有没有可能找出原因?调试,记录,什么?

更新

请注意,该脚本是作为Nuget包安装过程的一部分执行的.它可能非常特定于Nuget.

c# powershell visual-studio nuget

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

$ LastExitCode = 0,但在PowerShell中$?= False.将stderr重定向到stdout会产生NativeCommandError

为什么PowerShell在下面的第二个例子中显示出令人惊讶的行为?

首先,一个理智行为的例子:

PS C:\> & cmd /c "echo Hello from standard error 1>&2"; echo "`$LastExitCode=$LastExitCode and `$?=$?"
Hello from standard error
$LastExitCode=0 and $?=True
Run Code Online (Sandbox Code Playgroud)

没有惊喜.我打印一条消息到标准错误(使用cmd's echo).我检查变量$?和$LastExitCode.正如预期的那样,它们分别等于True和0.

但是,如果我要求PowerShell通过第一个命令将标准错误重定向到标准输出,我会得到一个NativeCommandError:

PS C:\> & cmd /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?"
cmd.exe : Hello from standard error
At line:1 char:4
+ cmd <<<<  /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?"
    + CategoryInfo          : NotSpecified: (Hello from …
Run Code Online (Sandbox Code Playgroud)

windows powershell command-line

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

如何在CMD中运行PowerShell

我正在尝试在cmd命令行中运行PowerShell脚本.有人给我一个例子,它有效:

powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
Run Code Online (Sandbox Code Playgroud)

但问题是我的PowerShell脚本有输入参数所以我试过但不起作用:

powershell.exe -noexit "& 'D:\Work\SQLExecutor.ps1 -gettedServerName "MY-PC" ' "
Run Code Online (Sandbox Code Playgroud)

错误是:

术语'D:\ Work\SQLExecutor.ps1 -gettedServerName"MY-PC"'无法识别为cmdlet函数的名称,

等待帮助!谢谢!

windows powershell cmd batch-file

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

如果尚未添加具有Add-Type -TypeDefinition的类,如何有条件地添加类?

请考虑以下PowerShell代码段:

$csharpString = @"
using System;

public sealed class MyClass
{
    public MyClass() { }
    public override string ToString() {
        return "This is my class. There are many others " +
            "like it, but this one is mine.";
    }
}
"@
Add-Type -TypeDefinition $csharpString;
$myObject = New-Object MyClass
Write-Host $myObject.ToString();
Run Code Online (Sandbox Code Playgroud)

如果我在同一个AppDomain中多次运行它(例如在powershell.exe或powershell_ise.exe中运行两次脚本),我会收到以下错误:

Add-Type : Cannot add type. The type name 'MyClass' already exists.
At line:13 char:1
+ Add-Type -TypeDefinition $csharpString;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (MyClass:String) [Add-Type],
 Exception
    + FullyQualifiedErrorId …
Run Code Online (Sandbox Code Playgroud)

powershell add-type

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

使用参数从Powershell脚本运行可执行文件

所以我有一个powershell脚本应该运行一个带有参数的可执行文件来传递以设置我想要运行的方法,我需要传递一个参数,这是一个配置文件的目录.所以这就是我的意思

Start-Process -FilePath "C:\Program Files\MSBuild\test.exe" -ArgumentList /genmsi/f $MySourceDirectory\src\Deployment\Installations.xml
Run Code Online (Sandbox Code Playgroud)

/ f是短名称,文件是我的属性的长名称...我在powershell中收到错误,告诉我无法找到/ f或/ file的位置参数.

有什么想法吗?

powershell command-line

43
推荐指数
4
解决办法
19万
查看次数

如何在PowerShell脚本中重用/导入脚本代码?

我必须创建一个与我以前的脚本完全相同的PowerShell脚本,但这次我必须读取CSV文件而不是XML文件.我的计划是创建一个PowerShell脚本,该脚本具有两个脚本所需的通用功能,并在两个主文件中重用此公共脚本文件.

假设我在创建2个目录2个主文件C:\的车程,让我的共同文件和其他3 届党库的文件夹中的D:\驱动器,例如C:\script_1_folder\Script1.ps1,C:\script_2_folder\Script2.ps1与常见的文件和3 分党库将在D:\script_common.

  1. 如何在主文件中调用\重用公共文件(如何获取路径,是否必须创建公共文件的实例以及如何使用它)

    有什么区别

    $script_path    = $myinvocation.invocationname;      
    $script_folder  = split-path $script_path -parent;
    write-host  $script_folder  
    $script_name    = split-path $script_path -leaf;      
    $current_folder = [system.io.directory]::getcurrentdirectory()    
    [system.io.directory]::setcurrentdirectory($script_folder)
    Set-Location $script_folder
    add-type -path ".\AlexFTPS-1.1.0\AlexPilotti.FTPS.Client.dll"
    
    Run Code Online (Sandbox Code Playgroud)

    和

    $path = (split-path $MyInvocation.MyCommand.Path) 
    $loggerPath = $path + "\Logger\release\Logger.ps1";
    .$loggerPath; 
    $logger = Logger;   
    $logger.load($path + "\Logger\config\log4ps.xml","log.log"); 
    
    Run Code Online (Sandbox Code Playgroud)

    对于我的问题,最好的方法是什么?

  2. 如何在Windows临时文件夹中创建临时文件夹?

powershell

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