2011-08-19 79 views
1

好日子,爲什麼我的PowerShell結果重複?

PowerShell的新人在這裏...

如果我輸出到屏幕這樣的:

foreach($databasePermission in $database.EnumDatabasePermissions($user.Name)) 
{ 
    Write-Host $databasePermission.PermissionState $databasePermission.PermissionType "TO" $databasePermission.Grantee 
} 

我得到這個,這是我想要的:

Grant CONNECT TO dbo 

但是,如果我嘗試輸出到像這樣的文本文件:

foreach($databasePermission in $database.EnumDatabasePermissions($user.Name)) 
{ 
    "$databasePermission.PermissionState $databasePermission.PermissionType TO $databasePermission.Grantee" | Out-File $filename 
} 

我的結果重複這樣的:

[dbname] Database: cust_serv, Grant, CONNECT.PermissionState [dbname] Database: cust_serv, Grant, CONNECT.PermissionType TO [dbname] Database: cust_serv, Grant, CONNECT.Grantee 

謝謝!

回答

3

如果您訪問字符串中的一個對象的屬性,你必須把它們放在$(...)

"$databasePermission.PermissionState" 

"$($databasePermission.PermissionState)" 
+0

謝謝!!!!!! –

2

好方式之間的區別構造長字符串是使用字符串格式。這是更可讀(IMO ATLEAST,因爲你沒有做$($...)等):

"{0} {1} TO {2}" -f $databasePermission.PermissionState,$databasePermission.PermissionType,$databasePermission.Grantee 
相關問題