使用powershell在linux发行版中获取文件名和文件扩展名

马马杜

我在 CentOS 上使用以下脚本来获取文件扩展名和文件名,没有扩展名和完整文件名,它在 Windows 上运行良好,但是当我在 ubuntu 上使用它时它不起作用,它显示了很多错误:

方法调用失败,因为 [System.IO.DirectoryInfo] 不包含名为“LastIndexOf”的方法。

方法调用失败,因为 [System.IO.DirectoryInfo] 不包含名为“Substring”的方法。

你可以在下面找到我写的代码

$files = get-childitem /etc/ssl/certs
foreach ($file in $Files)
{ 
# Nom du fichier avec extension
$pos_last_anti_slash = $file.LastIndexOf("\")
$fullname = $files.Substring($pos_last_anti_slash+1)
# Extension du fichier
$pos_last_point = $file.LastIndexOf(".")
$extension = $file.Substring($pos_last_point+1)
Write-Host $extension

# Nom du fichier sans extension
$filename = $file.Substring($pos_last_anti_slash+1)
$pos_last_points = $filename.LastIndexOf(".")
$filename = $filename.Substring(0,  $pos_last_points)
Write-Host $filename
if($filextension == "p7b")
{
openssl x509 -inform p7b -in /etc/ssl/certs/$fullname -out 
/etc/ssl/certs/$filename.pem
$var = ((& openssl x509 -in $file -dates -noout) -match 'notAfter')

}
}
点亮

使用Get-Membercmdlet 查看文件对象具有哪些字段。我认为 FullName、BaseName、Extension 和 Name 可能很有趣。

Get-ChildItem -File -Path '/etc/ssl/certs' | Get-Member

我没有测试过这个,但它可能接近你想要的。我确实替换==-eq.

$certsdir = '/etc/ssl/certs'
$files = Get-ChildItem -File -Path $certsdir
foreach ($file in $files) { 
    # Nom du fichier avec extension
    $fullname = $_.Name

    # Extension du fichier
    $extension = $_.Extension
    Write-Host $extension

    # Nom du fichier sans extension
    $filename = $_.BaseName
    Write-Host $filename

    if ($filextension -eq ".p7b") {
        openssl x509 -inform p7b -in $_.FullName -out $(Join-Path -Path $certsdir -ChildPath "$filename.pem")
        $var = ((& openssl x509 -in $file -dates -noout) -match 'notAfter')
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章