与Get-Item冲突

时间:2016-11-25 02:41:17

标签: sql powershell get-childitem

我有一行代码中存在一些冲突,我只是看不出它是什么。

这是程序

$Date = Get-Date -Format MM-dd-yyyy-HH-mm-ss-tt

<#*****SQL Session Variables*****#>
$Username = "sa"
$Password = "xyz" | ConvertTo-SecureString -asPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($Username,$Password)

$DatabaseNames = Invoke-SQLcmd -Query "Select * From sys.databases WHERE Name NOT LIKE '%Master%' AND Name NOT LIKE '%Tempdb%' AND Name NOT LIKE '%Model%' AND Name NOT LIKE '%Msdb%';" -ServerInstance "localhost" | Select Name -ExpandProperty Name
Write-Host "****Database Names****" -ForeGroundColor Red -BackGroundColor Black
$DatabaseNames
"`n`n"

<#****Get Database Size****#>
Push-Location
Import-Module sqlps -disablenamechecking
Invoke-Sqlcmd -Query "select name, physical_name, size * 8.0 / 1024 size from sys.master_files WHERE Type_Desc NOT LIKE '%LOG%' AND Name NOT LIKE '%Master%' AND Name NOT LIKE '%Tempdb%' AND Name NOT LIKE '%Model%' AND Name NOT LIKE '%Msdb%';" -ServerInstance "localhost" | Select @{Label="Database Name";Expression={$_.name}}, @{Label="Location";Expression={$_.physical_name}}, @{Label="SIze (MB)";Expression={$_.size}}
Pop-Location
"`n"
Start-Sleep 4

<#****Backup Databases****#>
ForEach ($DatabaseName in $DatabaseNames)
{
$SQLBackupPath1 = "C:\T2\SQlBackup\"
$SQLBackupPath2 = "$DatabaseName"
$SQLBackupPath3 = "_"
$SQLBackupPath4 = $Date
$SQLBackupPath5 = "_.bak"
$SQLBackupPath = $SQLBackupPath1 + $SQLBackupPath2 + $SQLBackupPath3 + $SQLBackupPath4 + $SQLBackupPath5
$SQLBackupPath

Backup-SQLDatabase -ServerInstance localhost -Database $DatabaseName -BackupFile $SQLBackupPath -Credential $Credentials
Write-Host "Database Backup of " -NoNewLine
Write-Host "$DatabaseName" -ForeGroundColor Red -BackGroundColor Black -NoNewLine
Write-Host " has been completed" 
"`n"
}

<#****Delete Databases****#>
ForEach ($DatabaseName in $DatabaseNames)
{
Push-Location
Import-Module sqlps -disablenamechecking
Invoke-Sqlcmd -Query "EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'$DatabaseName';" -ServerInstance "localhost"
Invoke-Sqlcmd -Query "USE [master];" -ServerInstance "localhost"
Invoke-Sqlcmd -Query "ALTER DATABASE [$DatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;" -ServerInstance "localhost"
Invoke-Sqlcmd -Query "DROP DATABASE [$DatabaseName];" -ServerInstance "localhost"
Pop-Location
Write-Host "Database: " -NoNewLine
Write-Host "$DatabaseName" -ForeGroundColor Red -BackGroundColor Black -NoNewLine
Write-Host " has been Dropped"
"`n"
}

Get-ChildItem -Path C:\T2\SQLBackup\ | Where-Object {$_.Name -match "ApOps*" } | Sort-Object LastWriteTime -Descending

如果我在第一行$ Date下移动最终的GetChildItem行,我会得到输出。但是,如果我在循环中使用它或它当前所在的位置,则不会返回任何结果。这让我相信代码中存在一些冲突的陈述。如果是这样,我似乎无法找到它,我正在失去理智寻找它。请帮忙。

1 个答案:

答案 0 :(得分:2)

您被输出格式化了,并且对输出有点粗心。考虑一下,我在一个包含几个文件的目录中:

列出文件,简单,工作正常:

myCar

输出一个空字符串,然后列出文件,简单并且工作正常。一开始就有一个空白行:

price

获取字符串长度,然后列出文件。等什么?输出格式化程序从它接收的第一个输出(字符串长度)获取您要求的属性,然后将其应用于它接收的所有内容(文件),直到管道为空:

myCar

获取一些不存在的属性,然后列出文件。这是同样的想法,只是这次它没有显示字符串或文件的任何内容,因为self在任何一种类型上都不存在:

PS D:\test> Get-ChildItem  # list the files


    Directory: D:\test

Mode                LastWriteTime         Length Name                                                                                                 
----                -------------         ------ ----                                                                                                 
-a----       02/11/2016     07:01           1640 log.txt                                                                                              
-a----       02/11/2016     07:01              0 testfile                                                                                             

您正在做这种事情 - 将混合类型发送到输出管道。他们仍然转到输出,如果你在变量中捕获它们或者将它们写入文件中数据就在那里,但输出格式化程序试图找出如何在控制台上显示它们,它会获得第一件事并格式化输出相反,假设你的输出一致。它来自你的代码的第一个复杂对象:

PS D:\test> ""; Get-ChildItem



    Directory: D:\test

Mode                LastWriteTime         Length Name                                                                                                 
----                -------------         ------ ----                                                                                                 
-a----       02/11/2016     07:01           1640 log.txt                                                                                              
-a----       02/11/2016     07:01              0 testfile                                                                                             

修正:

PS D:\test> "" | select Length; Get-ChildItem  # select string length, then...

Length     # missing columns ??!!
------     
     0     # string length
  1640     # file length
     0     # file length  

或使用更多Write-Host,或输出到文件。