将变量插入字符串

时间:2018-02-09 01:25:43

标签: powershell variables copy-item

PoSh更新,并且在将变量插入字符串时遇到了一些麻烦。我使用PowerShell Application Deployment Toolkit打包应用程序,这是我的脚本片段(应用程序中的Get-LoggedOnUser和Copy-File工作):

登录用户

           $user=Get-LoggedOnUser

复制plist文件

 Copy-File -Path "$dirSupportFiles\com.iliumsoft.ewallet.plist"
-destination "C:\Users\$user\AppData\Roaming\com.iliumsoft.ewallet.plist"

我查看了此处和网络上其他地方的一些帖子,大多数问题/解决方案将变量附加到路径的末尾。我和Join-Path一起玩,但也无法使用它。

1 个答案:

答案 0 :(得分:1)

所以我假设您正在使用此处的Get-LoggedOnUser脚本:

https://gallery.technet.microsoft.com/scriptcenter/Get-LoggedOnUser-Gathers-7cbe93ea

为了实现这一目标,最简单的方法是将其放入函数中:

function Get-LoggedOnUSer {
   << Script Code >>
}

然后将其调用变量:

$item = Get-LoggedOnUSer

对于复制文件,您将一个项目数组传递给$ user变量,因此您需要将其更改为:

$item = Get-LoggedOnUSer
$user = $item.UserName

Copy-Item -Path "$dirSupportFiles\com.iliumsoft.ewallet.plist" -destination "C:\Users\$user\AppData\Roaming\"