美好的一天,

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

谢谢!

最佳答案

如果使用字符串访问对象的属性,则必须将其包含在$(...)中:

看一下两者之间的区别

"$databasePermission.PermissionState"


"$($databasePermission.PermissionState)"

关于powershell - 为什么我的PowerShell结果重复?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7124259/

10-12 06:01