PowerShell输入文件格式

时间:2014-07-28 08:55:26

标签: powershell csv input

我实际上想询问您对输入文件的最佳格式的建议。

过去,我一直使用带有import-CSV的CSV格式。但正如您在下面的示例中所看到的(删除所有早于x天的文件),解密所有行是非常繁琐的。大块的代码几乎无法读取,但工作得很好:P

现在我被要求创建一个新脚本(从一个文件夹复制到另一个文件夹),它需要一个输入文件。例如,您可能稍后决定向输入文件添加更多开关。或者将特定开关留空。

你们如何处理输入文件?或者,您通常以何种方式格式化输入文件,以便以后在脚本中轻松解密?

一如既往,感谢您的建议。

示例输入文件:

# Input formats:
#
# [SERVER,]PATH[,OlderThanDays][ ,k]
# MailTo: [user1@domain.com] [user2@domain.com] [..]
#
# - PATH > Path to a file or folder that can be a UNC-path, local path on the script server or a local path on the remote server if you specify [SERVER, ]
# - [] > Fields between these brackets are optional
# - OlderThanDays > Nothing provided, a default of 30 days will be used
#                 > 0, zero means all files and folders will be deleted
# - k > If provided we don't do anything with folders, by default we will delete all empty folders regardless of their creation date
# - [SERVER] > Can be the name of the server or the name of the local host. Must have OS Windows Server 2008 R2 or Windows 7 at least, otherwise use UNC-path
# - MailTo:  > Contains the mail addresses of the people that want to receive a mail with the result
# - # can be used to comment out a line, this line will not be processed
#
# Notes:
# - User ScriptAdmin@domain.net needs write permissions to your path
# - For Windows Server 2003 and Windows XP we only support the UNC-path
# - Files are much faster deleted when using the server name and the local path, instead of the UNC-path
#
# Examples:
#
# SERVER1, C:\Appels.txt
# MailTo: Bob@domain.net Jake@domain.net Bobby@domain.net
# > The file 'Appels.txt' on the C-drive of SERVER1 will be removed when it's older than 30 days (default value).
# > A notification mail will be sent to Bob, Jake and Bobby afterwards with the results
#
# SERVER1, C:\Appels.txt, 0
# > The file 'Appels.txt' on the C-drive of SERVER1 will be removed every time the script runs, regardless of it's creation date.
#
# SERVER2, E:\SHARE\Target
# > All files older than 30 days will be deleted, all empty folders regardless of their creation date will be deleted to
#
# SERVER2, E:\SHARE\Target, 5
# > All files older than 5 days will be deleted, all empty folders regardless of their creation date will be deleted to
#
# SERVER2, E:\SHARE\Target, 15, k
# > All files older than 15 days will be deleted, we don't do anything with folders and leave them as is
#
# SERVER2, E:\SHARE\Target, 0
# > All files and folders will be deleted
#
# SERVER2, E:\SHARE\Target, 0, k
# > All files will be deleted, we don't do anything with folders and leave them as is
#
# \\domain.net\SHARE\Target
# > All files older than 30 days will be deleted, all empty folders regardless of their creation date will be deleted to
#
# E:\SHARE\Target, 10
# > All files older than 10 days will be deleted and all empty folders regardless of their creation date will be deleted to on the local host
# ______________________________________________________________________

脚本中的代码:

$File = (Import-Csv -Path $ImportFile -Header "A", "B", "C", "D" | Where { $_.A -NotLike "#*" })
Foreach ($_ in $File) {

# Mail recipients
if ($_.A -like "MailTo:*") {
    $MailTo += $_.A -replace "^MailTo: " | foreach {$_.Split(" ")}
    Continue # to the next object, this line doesn't need further processing for '$arrayAllPaths'
}

# UNC path
elseif ($_.A -like "\\*") {
    $Server="UNC"
    $Target=$_.A
    if(!$_.B) { 
        # if B is empty
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$true
    } 
    elseif ($_.B -eq "k") {
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$false
    } 
    elseif ($_.B -eq "0") {
        if (!$_.C) {
            $OlderThanDays="0"
            $CleanFolders=$true
         } 
         elseif ($_.C -eq "k") {
                $OlderThanDays="0"
                $CleanFolders=$false
         }
    } 
    else {
        if($_.C -eq "k") {
           $CleanFolders=$false
           $OlderThanDays=$_.B
        } 
        else {
        $CleanFolders=$true
        $OlderThanDays=$_.B
        }
    }
}

# Local path
elseif ($_.A -like "[A-Z]:*") {    
    $Server="$env:COMPUTERNAME"
    $Target=$_.A
     if(!$_.B) { 
        # if B is empty
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$true
    } 
    elseif ($_.B -eq "k") {
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$false
    } 
    elseif ($_.B -eq "0") {
        if (!$_.C) {
            $OlderThanDays="0"
            $CleanFolders=$true
         } 
         elseif ($_.C -eq "k") {
                $OlderThanDays="0"
                $CleanFolders=$false
         }
    } 
    else {
        if($_.C -eq "k") {
           $CleanFolders=$false
           $OlderThanDays=$_.B
        } 
        else {
        $CleanFolders=$true
        $OlderThanDays=$_.B
        }
    }
}

# Local host with local path
elseif ($_.A -eq $env:COMPUTERNAME) {    
    $Server="$env:COMPUTERNAME"
    $Target=$_.B
      if(!$_.C) { 
        # if C is empty
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$true
    } 
    elseif ($_.C -eq "k") {
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$false
    } 
    elseif ($_.C -eq "0") {
        if (!$_.D) {
            $OlderThanDays="0"
            $CleanFolders=$true
         } 
         elseif ($_.D -eq "k") {
                $OlderThanDays="0"
                $CleanFolders=$false
         }
    } 
    else {
        if($_.D -eq "k") {
           $CleanFolders=$false
           $OlderThanDays=$_.C
        } 
        else {
        $CleanFolders=$true
        $OlderThanDays=$_.C
        }
    }        
}

# Remote servers with local path
else {
    $Server=$_.A
    $Target=$_.B
      if(!$_.C) { 
        # if C is empty
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$true
    } 
    elseif ($_.C -eq "k") {
        $OlderThanDays=$DefaultOlderThanDays
        $CleanFolders=$false
    } 
    elseif ($_.C -eq "0") {
        if (!$_.D) {
            $OlderThanDays="0"
            $CleanFolders=$true
         } 
         elseif ($_.D -eq "k") {
                $OlderThanDays="0"
                $CleanFolders=$false
         }
    } 
    else {
        if($_.D -eq "k") {
           $CleanFolders=$false
           $OlderThanDays=$_.C
        } 
        else {
        $CleanFolders=$true
        $OlderThanDays=$_.C
        }
    }        
}

# All server names in upper case 
if ($Server) {
    $Server = $Server.ToUpper() 
}

# Target first letters uppercase the rest in lowercase
if ($Target -like "`\`\*" ) {
    $Target = $Target.Substring(0,2)+$Target.Substring(2,1).ToUpper()+$Target.Substring(3).ToLower()
} 
else {
      $Target = $Target.Substring(0,1).ToUpper()+$Target.Substring(1,2)+$Target.Substring(3,1).ToUpper()+$Target.Substring(4).ToLower()
}
}

1 个答案:

答案 0 :(得分:0)

我认为您的错误是将输入文件与实际执行清理作业的代码组合在一起。所以我建议的第一件事就是将所有执行实际工作的代码提取到cmdlet中(并将其放在Powershell模块文件中,以便可以根据需要导入它)。

通过编写cmdlet,您可以获得自动生成的文档,您可以解析和验证为您完成的参数,并且您可以集中精力确保代码完成您所需的操作,而无需担心输入文件格式等问题。您可以将上面给出的示例移到cmdlet文档的.EXAMPLES部分。 Cmdlet还可以支持-WhatIf参数之类的内容,这样人们就可以在不实际删除的情况下测试删除的内容。

然后您的输入文件可以只是一个powershell脚本,它使用适当的参数调用cmdlet:

Remove-UnwantedFiles -server SERVER1 -Path C:\Appels.txt -MailTo "Bob@domain.net","Jake@domain.net","Bobby@domain.net"
# The file 'Appels.txt' on the C-drive of SERVER1 will be removed when it's older than 30 days (default value).
# A notification mail will be sent to Bob, Jake and Bobby afterwards with the results

Remove-UnwantedFiles -server SERVER1 -Path C:\Appels.txt -MaxAge 0
# The file 'Appels.txt' on the C-drive of SERVER1 will be removed every time the script runs, regardless of it's creation date.
#

Remove-UnwantedFiles -server SERVER2 -Path E:\SHARE\Target
# > All files older than 30 days will be deleted, all empty folders regardless of their creation date will be deleted to

依旧......

或者您可以解析一些csv并使用它来调用cmdlet,或者读取一些json或xml作为输入。