Powershell - "无效的赋值表达式。"错误

时间:2017-03-31 03:55:41

标签: powershell

我有一些我正在做的事情。其中一个是我在这里找到的一种进口/出口产品。但我收到以下错误

  

PS C:\ Users \ joeblogs> C:\ Users \ joeblogs \ Scripts \ Copy user data.ps1无效的赋值表达式。作业的左侧   运算符需要是可以像变量一样分配的东西   或财产。在C:\ Users \ delpillay \ Documents \ Scripts \ Copy用户   data.ps1:16 char:12   + $ username =<<<< gc env:userame       + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException       + FullyQualifiedErrorId:InvalidLeftHandSide

我不知道从哪里开始,我不知道该尝试什么......

以下是代码:

$destination = "E:\Users\%username%\Backup"
$folder = "Desktop",
#"Downloads",
"Favorites",
"My Documents",
#"Music",
#"Pictures",
#"Videos",
#"AppData\Local\Mozilla",
#"AppData\Local\Google",
#"AppData\Roaming\Mozilla"

######################################

$username = gc env:userame
$userprofile = gc env:userprofile
##$appData = gc env:localAPPDATA


###### Restore data section ######
if ([IO.Directory]::Exists($destination + "\" + $username + "\")) 
{ 

$caption = "Choose Action";
$message = "A backup folder for $username already exists, would you like to      restore the data to the local machine?";
$Yes = new-Object System.Management.Automation.Host.ChoiceDescription  "&Yes","Yes";
$No = new-Object System.Management.Automation.Host.ChoiceDescription "&No","No";
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No);
$answer = $host.ui.PromptForChoice($caption,$message,$choices,0)

if ($answer -eq 0) 
{

    write-host -ForegroundColor green "Restoring data to local machine for  $username"
    foreach ($f in $folder)
    {   
        $currentLocalFolder = $userprofile + "\" + $f
        $currentRemoteFolder = $destination + "\" + $username + "\" + $f
        write-host -ForegroundColor cyan "  $f..."
        Copy-Item -ErrorAction silentlyContinue -recurse $currentRemoteFolder $userprofile

        if ($f -eq "AppData\Local\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
        if ($f -eq "AppData\Roaming\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
        if ($f -eq "AppData\Local\Google") { rename-item $currentLocalFolder "$currentLocalFolder.old" }

    }
    rename-item "$destination\$username" "$destination\$username.restored"
    write-host -ForegroundColor green "Restore Complete!"
}

else
{
    write-host -ForegroundColor yellow "Aborting process"
    exit
}


}

###### Backup Data section ########
#else 
{ 

Write-Host -ForegroundColor green "Outlook is about to close, save any unsaved emails then press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Get-Process | Where { $_.Name -Eq "OUTLOOK" } | Kill

write-host -ForegroundColor green "Backing up data from local machine for $username"

foreach ($f in $folder)
{   
    $currentLocalFolder = $userprofile + "\" + $f
    $currentRemoteFolder = $destination + "\" + $username + "\" + $f
    $currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
    $currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
    write-host -ForegroundColor cyan "  $f... ($currentFolderSizeRounded MB)"
    Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder
}



$oldStylePST = [IO.Directory]::GetFiles($appData + "\Microsoft\Outlook", "*.pst") 
foreach($pst in $oldStylePST)   
{ 
    if ((test-path -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")) -eq 0){new-item -type directory -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle") | out-null}
    write-host -ForegroundColor yellow "  $pst..."
    Copy-Item $pst ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")
}    

write-host -ForegroundColor green "Backup complete!"

} 

1 个答案:

答案 0 :(得分:1)

很少有观察结果:

您没有评论收藏夹我的文档。如果您想使用它们,请直接使用逗号分隔。

使用此:

$destination = "E:\Users\%username%\Backup"
$folder = "Desktop","Favorites","My Documents"
#"Downloads",
#"Favorites",
#"My Documents",
#"Music",
#"Pictures",
#"Videos",
#"AppData\Local\Mozilla",
#"AppData\Local\Google",
#"AppData\Roaming\Mozilla" 

您错过了用户名中的 n

$username = gc env:username

不要使用gc env:username。而是直接访问它们,如下所示:完全重新构建:

$destination = "E:\Users\%username%\Backup"
$folder = "Desktop","Favorites","My Documents"
#"Downloads",
#"Favorites",
#"My Documents",
#"Music",
#"Pictures",
#"Videos",
#"AppData\Local\Mozilla",
#"AppData\Local\Google",
#"AppData\Roaming\Mozilla"

######################################

$username = $env:username
$userprofile = $env:userprofile
##$appData = gc env:localAPPDATA

这些是已修复的主要内容。希望它可以帮到你。