我希望能够在我的一些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中推荐?
当我在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)
每时每刻.
在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)
会非常方便.
在PowerShell中,Out-File和之间的区别是Set-Content什么?还是Add-Content和Out-File -append?
我发现如果我对同一个文件使用两者,文本完全是mojibaked.
(小问题:>是别名Out-File,对吧?)
我可以使用此命令递归获取所有子项:
Get-ChildItem -recurse
Run Code Online (Sandbox Code Playgroud)
但有没有办法限制深度?例如,如果我只想降低一个或两个级别?
如何C:\Users\User\AppData\Roaming在PowerShell中获取应用程序数据目录(例如)的路径?
如何测试对象是否具有特定属性?
欣赏我能做到......
$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下载9频道,但我尝试过的脚本有错误:
这个脚本
$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参数,然后重试.
我也试过这个
# --- 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脚本来执行文件复制.当我想使用过滤器包含多个文件类型时出现问题.
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) $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中的确是什么/意味着什么?