标签: powershell

如何在PowerShell中创建自定义类型以供我的脚本使用?

我希望能够在我的一些PowerShell脚本中定义和使用自定义类型.例如,让我假装我需要一个具有以下结构的对象:

Contact
{
    string First
    string Last
    string Phone
}
Run Code Online (Sandbox Code Playgroud)

我将如何创建它,以便我可以在以下功能中使用它:

function PrintContact
{
    param( [Contact]$contact )
    "Customer Name is " + $contact.First + " " + $contact.Last
    "Customer Phone is " + $contact.Phone 
}
Run Code Online (Sandbox Code Playgroud)

这样的事情是可能的,甚至在PowerShell中推荐?

powershell scripting

75
推荐指数
5
解决办法
7万
查看次数

在PowerShell中等效于C#的"using"关键字?

当我在C#中的.net-Framework中使用另一个对象时,我可以使用using指令节省大量的输入.

using FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It;

...


  var blurb = new Thingamabob();

...
Run Code Online (Sandbox Code Playgroud)

那么在Powershell中有一种方法可以做类似的事情吗?我正在访问很多.net对象,并且不喜欢打字

 $blurb = new-object FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It.Thingamabob;
Run Code Online (Sandbox Code Playgroud)

每时每刻.

.net powershell powershell-2.0

74
推荐指数
6
解决办法
4万
查看次数

Powershell:仅为单个命令设置环境变量

在Linux上,我可以这样做:

$ FOO=BAR ./myscript
Run Code Online (Sandbox Code Playgroud)

在设置环境变量FOO的情况下调用"myscript".

在Powershell中是否有类似的可能,即无需先设置变量,调用命令,然后再次取消设置变量?

为了更清楚我的用例 - 我不想将它作为脚本的一部分使用.相反,我有一个第三方脚本,其行为我可以使用环境变量控制,但在这种情况下,不是命令行参数.因此能够在打字之间交替

$ OPTION=1 ./myscript
Run Code Online (Sandbox Code Playgroud)

和

$ ./myscript
Run Code Online (Sandbox Code Playgroud)

会非常方便.

windows variables environment powershell scripting

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

PowerShell Set-Content和Out-File - 有什么区别?

在PowerShell中,Out-File和之间的区别是Set-Content什么?还是Add-Content和Out-File -append?

我发现如果我对同一个文件使用两者,文本完全是mojibaked.

(小问题:>是别名Out-File,对吧?)

powershell

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

限制Get-ChildItem递归深度

我可以使用此命令递归获取所有子项:

Get-ChildItem -recurse
Run Code Online (Sandbox Code Playgroud)

但有没有办法限制深度?例如,如果我只想降低一个或两个级别?

powershell

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

在PowerShell中获取%AppData%的路径

如何C:\Users\User\AppData\Roaming在PowerShell中获取应用程序数据目录(例如)的路径?

powershell scripting

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

如何测试对象是否具有特定属性?

如何测试对象是否具有特定属性?

欣赏我能做到......

$members = Get-Member -InputObject $myobject 
Run Code Online (Sandbox Code Playgroud)

然后foreach通过$members,但是有一个函数来测试对象是否具有特定的属性?

附加信息: 问题是我正在导入两种不同类型的CSV文件,一种是两列,另一种是三列.我无法使用"Property"进行检查,只能使用"NoteProperty"... 无论差异是什么

if ( ($member.MemberType -eq "NoteProperty" ) -and ($member.Name -eq $propertyName) ) 
Run Code Online (Sandbox Code Playgroud)

powershell powershell-3.0

73
推荐指数
8
解决办法
8万
查看次数

无法解析响应内容,因为Internet Explorer引擎不可用,或者

我需要使用powershell下载9频道,但我尝试过的脚本有错误:

  1. 这个脚本

    $url="https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high"
    $rss=invoke-webrequest -uri $url 
    $destination="D:\Videos\OfficePnP"
    [xml]$rss.Content|foreach{ 
      $_.SelectNodes("rss/channel/item/enclosure") 
    }|foreach{ 
        "Checking $($_.url.split("/")[-1]), we will skip it if it already exists in $($destination)"
      if(!(test-path ($destination + $_.url.split("/")[-1]))){ 
        "Downloading: " + $_.url 
        start-bitstransfer $_.url $destination 
      } 
    }
    
    Run Code Online (Sandbox Code Playgroud)

    失败了,错误:

    无法解析响应内容,因为Internet Explorer引擎不可用,或者Internet Explorer的首次启动配置未完成.指定UseBasicParsing参数,然后重试.

  2. 我也试过这个

    # --- settings ---
    $feedUrl = "https://channel9.msdn.com/blogs/OfficeDevPnP/feed/mp4high"
    $mediaType = "mp4high"
    $overwrite = $false
    $destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "OfficeDevPnP"
    
    # --- locals ---
    $webClient = New-Object System.Net.WebClient
    
    # --- functions ---
    function PromptForInput ($prompt, $default) {
     $selection = read-host "$prompt`r`n(default: $default)" …
    Run Code Online (Sandbox Code Playgroud)

powershell internet-explorer

73
推荐指数
7
解决办法
6万
查看次数

如何在PowerShell复制脚本中正确过滤多个字符串

我正在使用此答案中的PowerShell脚本来执行文件复制.当我想使用过滤器包含多个文件类型时出现问题.

Get-ChildItem $originalPath -filter "*.htm"  | `
   foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` 
 New-Item -ItemType File -Path $targetFile -Force;  `
 Copy-Item $_.FullName -destination $targetFile }
Run Code Online (Sandbox Code Playgroud)

像梦一样工作.但是,当我想使用过滤器包含多个文件类型时,会出现问题.

Get-ChildItem $originalPath ` 
  -filter "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*")  | `
   foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` 
 New-Item -ItemType File -Path $targetFile -Force;  `
 Copy-Item $_.FullName -destination $targetFile }
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported.
At F:\data\foo\CGM.ps1:121 char:36
+ Get-ChildItem $originalPath …
Run Code Online (Sandbox Code Playgroud)

powershell copy filter powershell-2.0

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

powershell中的&符号是什么意思?

$tool = 'C:\Program Files\gs\gs9.07\bin\gswin64c.exe'

& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $pdf.FullName -c quit
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释这是如何工作的吗?&符号在powershell中的确是什么/意味着什么?

powershell

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