我有一系列字符串,它们是文件的完整路径.我只想保存文件名,没有文件扩展名和前导路径.所以从这个:
c:\temp\myfile.txt
Run Code Online (Sandbox Code Playgroud)
至
myfile
Run Code Online (Sandbox Code Playgroud)
我实际上并没有遍历一个目录,在这种情况下basename可以使用类似PowerShell 属性的东西,而是我只处理字符串.
有没有办法改变Powershell的默认位置?
你如何设置Powershell的默认工作目录?
我有一个脚本,我在$ args中传递服务器名称.
这样我可以使用以下方法对这个(这些)服务器执行以下操作foreach:
.\script.ps1 host1 host2 host3
foreach ($i in $args)
{
Do-Stuff $i
}
Run Code Online (Sandbox Code Playgroud)
我想添加一个名为vlan的命名可选参数.我试过了:
Param(
[string]$vlan
)
foreach ($i in $args)
{
Write-Host $i
}
Write-Host $vlan
Run Code Online (Sandbox Code Playgroud)
如果您传递-vlan参数,它会起作用,但如果您不传递参数,则脚本会自动将最后一个服务器名称分配给$vlan.
那么,如何将单个或多个参数以及可选的命名参数传递给PowerShell脚本?
理想情况下,这是有效的例子:
.\script.ps1 host1
.\script.ps1 host1 host2 host3
.\script.ps1 host1 host2 -vlan office
Run Code Online (Sandbox Code Playgroud) 我已使用以下查询列出Windows 2008服务器中的用户,但失败并得到以下错误.
$server='client-pc-1';$pwd= convertto-securestring 'password$' -asplaintext -
force;$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist 'Administrator',$pwd; invoke-command -computername $server -credential
$cred -scriptblock {Get-ADUser -Filter (enabled -ne $true)}
Run Code Online (Sandbox Code Playgroud)
例外如下:任何人都可以帮我解决这个问题吗?
The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct
and try again.
+ CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud) 我正在使用一些多GB的文本文件,并希望使用PowerShell对它们进行一些流处理.这很简单,只需解析每一行并提取一些数据,然后将其存储在数据库中.
不幸的是,get-content | %{ whatever($_) }似乎在管道的这个阶段保持整个行集在内存中.它的速度也非常慢,需要花费很长时间才能完全阅读.
所以我的问题是两部分:
get-content进行迭代的PowerShell 似乎比C#脚本慢100倍.我希望我在这里做一些愚蠢的事情,比如错过一个-LineBufferSize参数或什么......
注意:PowerShell 1.0
我想获取当前正在执行的PowerShell文件名.也就是说,如果我像这样开始我的会话:
powershell.exe .\myfile.ps1
Run Code Online (Sandbox Code Playgroud)
我想得到字符串".\ myfile.ps1"(或类似的东西).编辑:"myfile.ps1"是首选.
有任何想法吗?
在我的批处理文件中,我像这样调用PowerShell脚本:
powershell.exe "& "G:\Karan\PowerShell_Scripts\START_DEV.ps1"
Run Code Online (Sandbox Code Playgroud)
现在,我想传递一个字符串参数START_DEV.ps1.让我们说参数是w=Dev.
我怎样才能做到这一点?
我正在使用这个(简化的)代码块从SQL Server中使用bcp提取一组表.
$OutputDirectory = "c:\junk\"
$ServerOption = "-SServerName"
$TargetDatabase = "Content.dbo."
$ExtractTables = @(
"Page"
, "ChecklistItemCategory"
, "ChecklistItem"
)
for ($i=0; $i -le $ExtractTables.Length – 1; $i++) {
$InputFullTableName = "$TargetDatabase$($ExtractTables[$i])"
$OutputFullFileName = "$OutputDirectory$($ExtractTables[$i])"
bcp $InputFullTableName out $OutputFullFileName -T -c $ServerOption
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但现在有些表需要通过视图提取,有些则不需要.所以我需要一个像这样的数据结构:
"Page" "vExtractPage"
, "ChecklistItemCategory" "ChecklistItemCategory"
, "ChecklistItem" "vExtractChecklistItem"
Run Code Online (Sandbox Code Playgroud)
我在看哈希,但我找不到任何关于如何循环哈希的东西.这里做什么是正确的?也许只是使用一个数组,但两个值都用空格分隔?
还是我错过了一些明显的东西?
我正在编写一个我想从服务器A运行的PowerShell脚本.我想连接到服务器B并将文件作为备份复制到服务器A.
如果无法完成,那么我想从服务器A连接到服务器B并将文件复制到服务器B中的另一个目录.
我看到他们Copy-Item命令,但我不知道如何给它一个计算机名称.
我原本以为我可以做点什么
Copy-Item -ComputerName ServerB -Path C:\Programs\temp\test.txt -Destination (not sure how it would know to use ServerB or ServerA)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在寻找检查Com对象是否存在的最佳方法.
这是我的代码; 我想改进最后一行:
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate("http://www.stackoverflow.com")
$ie.Visible = $true
$ie -ne $null #Are there better options?
Run Code Online (Sandbox Code Playgroud)