Powershell 对文件夹中的所有文件执行命令
我有一个要使用Powershell 对文件夹中的所有文件执行命令,powershell,putty,Powershell,Putty,我有一个要使用winscp.com转换为.ppk的.pem文件列表 命令是: WinSCP.com /keygen filename.pem /output=filename.ppk 如何编写脚本,自动读取文件夹中的所有*.pem文件并将其转换为ppk 我想我需要先用这个 Get-ChildItem -Recurse -Include "*.pem" | % { & $_ } 但是如何捕获文件名并使其替换命令中的行并处理文件夹中的所有几十个pem文件?这将为您提供文件名和全名。不同之
winscp.com
转换为.ppk的.pem文件列表
命令是:
WinSCP.com /keygen filename.pem /output=filename.ppk
如何编写脚本,自动读取文件夹中的所有*.pem文件并将其转换为ppk
我想我需要先用这个
Get-ChildItem -Recurse -Include "*.pem" | % { & $_ }
但是如何捕获文件名并使其替换命令中的行并处理文件夹中的所有几十个pem文件?这将为您提供文件名和全名。不同之处在于,全名包含完整的路径,而名称只是文件名
Get-ChildItem c:\users\somefolder -Recurse -filter "*.pem" | foreach{
$name = $_.Name
$fullName = $_.FullName
Write-Output $name
Write-Output $fullName
}
如果你运行这个,你就会明白。因此,您可以获得这样的名称,然后在foreach循环中使用$name变量运行任何其他命令。尝试类似的方法
$msbuild = "C:\pathofexefile\WinSCP.com"
$formatstring="/keygen {0} /output={1}.ppk"
Set-Location "c:\temp"
gci -file -Filter "*.txt" | %{$arguments=($formatstring -f $_.FullName, $_.BaseName);start-process $msbuild $arguments }
Get-ChildItem -Recurse -Include "*.pem" | % {
$outfile = Join-Path $_.DirectoryName ($_.BaseName + '.ppk')
& winscp.com /keygen $_.FullName "/output=$outfile"
}
&
是PowerShell$\uu
是一个将当前对象保存在管道中的对象,在本例中为对象。对于这种类型的对象,表达式&$
调用文件上的默认处理程序(关联程序),就像在资源管理器中双击文件一样
要让代码执行您想要的操作,您需要用winscp.com
语句替换$\ucode>,并用适当的变量替换文件名文本。使用$\.FullName
作为文件的全名。更改输出文件的扩展名,并将该路径放入其他变量中
Get-ChildItem -Recurse -Include "*.pem" | % {
$outfile = Join-Path $_.DirectoryName ($_.BaseName + '.ppk')
& winscp.com /keygen $_.FullName "/output=$outfile"
}
很有魅力-谢谢!刚刚在winscp.com位置添加了一个变量,它将转换$scpfile