首页 文章

Powershell新项目目录,其中参数作为文件夹名称的一部分

提问于
浏览
1

我想根据名称中使用的参数创建一个文件夹,例如:

$Website = "test"
    new-item \\172.1.1.1\C`$\Folder\$Website_Archive -type directory

_Archive需要成为文件夹名称的一部分,但我无法弄清楚如何通过包含_Archive的$ Website .

这是我得到的错误:

New-Item:具有指定名称\ 172.1.1.1 \ C $ \ Folder \的项目已存在 . 在行:2 char:9 new-item <<<< \ 172.1.1.1 \ C` $ \ Folder \ $ Website_Archive -type directory CategoryInfo:ResourceExists:(\ 172.1.1.1 \ C \ Folder \:String)[New- Item],IOException FullyQualifiedErrorId:DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

如果我从行中删除'_Archive',它将创建名为“test”的文件夹 . 我假设它正在寻找$ Website_Archive的参数而没有找到它所以它试图重新创建根文件夹 . 如何将参数传递到文件夹名称?

谢谢!

BB

3 回答

  • 3

    只需用反推就可以逃避下划线

    new-item "\\17.1.1.1\C`$\Folder\$Website`_Archive" -type directory
    
  • 4

    根据JNK的答案逃脱,用 $(...) 附上$ Website

    new-item "\\17.1.1.1\C`$\Folder\$($Website)_Archive" -type directory
    
  • 3

    除了@JohnL提到的内容之外,我更喜欢字符串格式化,特别是涉及到许多变量时:

    "\\172.1.1.1\C`$\Folder\{0}_Archive" -f $Website
    

相关问题