在Powershell中检查某个用户是否具有导入的CSV文件中的特定属性

时间:2015-07-29 08:02:18

标签: powershell

我有以下关机脚本:

    Stop-Process -ProcessName Thunderbird* -Force -ErrorAction 'silentlycontinue' 
Stop-Process -ProcessName Outlook* -Force -ErrorAction 'silentlycontinue'
$personname= ([adsi]"WinNT://$env:userdomain/$env:username,user").fullname 
$res=Test-Path "\\cd2012\backup\$personname" 
if ($res -eq $False) 
{
    New-Item -ItemType directory -Path "\\cd2012\backup\$personname" 

    Import-Csv \\cd2012\backup\lista.csv -Encoding UTF8 -Delimiter ";" -Header Name,User,Attribute

                if  ($_.Attribute -ne "T" -and $_.User -eq $env:username) 
                    {
                        & c:\windows\system32\Robocopy.exe "$env:LOCALAPPDATA\Microsoft\Outlook\" "\\cd2012\backup\$personname" /E /MT:20 /R:50 /W:10 /V /ETA 
                    } 
                else
                    {
                        & c:\windows\system32\Robocopy.exe "$env:APPDATA\Thunderbird\Profiles\" "\\cd2012\backup\$personname" /E /MT:20 /R:50 /W:10 /V /ETA 

                    }
                Set-ItemProperty -Path "\\cd2012\backup\$personname" -Name LastWriteTime -Value $(Get-Date)  





}
else
{
    $wts=Get-Item "\\cd2012\backup\$personname" | Foreach {$_.LastWriteTime}  

        if ($wts -gt (Get-Date).AddDays(2)) 
        {
                Import-Csv \\cd2012\backup\lista.csv -Encoding UTF8 -Delimiter ";" -Header Name,User,Attribute

                if  ($_.Attribute -ne "T" -and $_.User -eq $env:username) 
                    {
                        & c:\windows\system32\Robocopy.exe "$env:LOCALAPPDATA\Microsoft\Outlook\" "\\cd2012\backup\$personname" /E /MT:20 /R:50 /W:10 /V /ETA 
                    } 
                else
                    {
                        & c:\windows\system32\Robocopy.exe "$env:APPDATA\Thunderbird\Profiles\" "\\cd2012\backup\$personname" /E /MT:20 /R:50 /W:10 /V /ETA 

                    }
                Set-ItemProperty -Path "\\cd2012\backup\$personname" -Name LastWriteTime -Value $(Get-Date)  


          }

}

CSV文件包含以下内容

鲍勃;用户1;

约翰;用户2;

麦克;用户3;吨

乔治:用户4;吨

此脚本基本上每2天制作一次文件夹备份。我遇到的问题是在确定客户端是否具有Outlook或Thunderbird时检查用户名和属性。如果用户具有“T”属性,我知道他有Thunderbird,所以我应该从Thunderbird位置复制。如果他没有,我应该从Outlook位置复制。

我知道我的所有客户都有相同的默认位置,因为我是唯一配置它的人。

1 个答案:

答案 0 :(得分:0)

您可以将脚本简化为以下内容:

Stop-Process -ProcessName Thunderbird* -Force -ErrorAction 'silentlycontinue' 
Stop-Process -ProcessName Outlook* -Force -ErrorAction 'silentlycontinue'

$personname= ([adsi]"WinNT://$env:userdomain/$env:username,user").fullname 

$robocopyExe = join-Path $env:windir 'system32\Robocopy.exe'
$backupFolder = Join-Path '\\cd2012\backup\' $personname

# ensure the backup target is present
New-Item -ItemType directory -Path $backupFolder -Force | out-null

$thunderbirdDir = Join-Path $env:APPDATA 'Thunderbird\Profiles'
$outlookDir = Join-Path $env:LOCALAPPDATA 'Microsoft\Outlook'

if (Test-Path $thunderbirdDir)
{
    & $robocopyExe $thunderbirdDir $backupFolder /E /MT:20 /R:50 /W:10 /V /ETA /minage:2
}
if (Test-Path $outlookDir)
{
    & $robocopyExe $outlookDir $backupFolder /E /MT:20 /R:50 /W:10 /V /ETA /minage:2
}

注意:使用Robocopy / minage 参数控制两天的复制跳过。