PoshCode Logo PowerShell Code Repository

SqlProxy.psm1 by Chad Miller 34 months ago (modification of post by Chad Miller view diff)
View followups from Chad Miller | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3040"></script>download | new post

Based on functions in SQLPSX. SqlProxy.psm1 module is used for administering SQL Server logins, users, and roles. Designed to be used with PS Remoting. All actions audited to a custom Eventlog. See Write-SqlProxyLog for log setup details

  1. # ---------------------------------------------------------------------------
  2. ### <Author>
  3. ### Chad Miller
  4. ### </Author>
  5. ### <Description>
  6. ### Based on functions in SQLPSX. SqlProxy.psm1 module is used for administering
  7. ### SQL Server logins, users, and roles. Designed to be used with PS Remoting.
  8. ### All actions audited to a custom Eventlog.
  9. ### See Write-SqlProxyLog for log setup details
  10. ### </Description>
  11. # ---------------------------------------------------------------------------
  12. try {add-type -AssemblyName "Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -EA Stop}
  13. catch {add-type -AssemblyName "Microsoft.SqlServer.ConnectionInfo"}
  14.  
  15. try {add-type -AssemblyName "Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -EA Stop; $smoVersion = 10}
  16. catch {add-type -AssemblyName "Microsoft.SqlServer.Smo"; $smoVersion = 9}
  17.  
  18. try {add-type -AssemblyName "Microsoft.SqlServer.SqlEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -EA Stop; $smoVersion = 10}
  19. catch {add-type -AssemblyName "Microsoft.SqlServer.SqlEnum"; $smoVersion = 9}
  20.  
  21. try
  22. {
  23.     try {add-type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -EA Stop}
  24.     catch {add-type -AssemblyName "Microsoft.SqlServer.SMOExtended" -EA Stop}
  25. }
  26. catch {Write-Warning "SMOExtended not available"}
  27.  
  28. $EventID = @{
  29. "Add-SqlDatabaseRoleMember"=0
  30. "Add-SqlLogin"=1
  31. "Add-SqlServerRoleMember"=2
  32. "Add-SqlUser"=3
  33. "Remove-SqlDatabaseRoleMember"=4
  34. "Remove-SqlLogin"=5
  35. "Remove-SqlServerRoleMember"=6
  36. "Remove-SqlUser"=7
  37. "Rename-SqlLogin"=8
  38. "Set-SqlLogin"=9
  39. "Set-SqlLoginDefaultDatabase"=10
  40. }
  41.  
  42. #######################
  43. <#
  44. .SYNOPSIS
  45. Gets a ServerConnection.
  46. .DESCRIPTION
  47. The Get-SqlConnection function  gets a ServerConnection to the specified SQL Server.
  48. .INPUTS
  49. None
  50.     You cannot pipe objects to Get-SqlConnection
  51. .OUTPUTS
  52. Microsoft.SqlServer.Management.Common.ServerConnection
  53.     Get-SqlConnection returns a Microsoft.SqlServer.Management.Common.ServerConnection object.
  54. .EXAMPLE
  55. Get-SqlConnection "Z002\sql2K8"
  56. This command gets a ServerConnection to SQL Server Z002\SQL2K8.
  57. .EXAMPLE
  58. Get-SqlConnection "Z002\sql2K8" "sa" "Passw0rd"
  59. This command gets a ServerConnection to SQL Server Z002\SQL2K8 using SQL authentication.
  60. .LINK
  61. Get-SqlConnection
  62. #>
  63. function Get-SqlConnection
  64. {
  65.     param(
  66.     [Parameter(Position=0, Mandatory=$true)] [string]$sqlserver,
  67.     [Parameter(Position=1, Mandatory=$false)] [string]$username,
  68.     [Parameter(Position=2, Mandatory=$false)] [string]$password
  69.     )
  70.  
  71.     Write-Verbose "Get-SqlConnection $sqlserver"
  72.    
  73.     try {
  74.         if($Username -and $Password)
  75.         { $con = new-object ("Microsoft.SqlServer.Management.Common.ServerConnection") $sqlserver,$username,$password }
  76.         else
  77.         { $con = new-object ("Microsoft.SqlServer.Management.Common.ServerConnection") $sqlserver }
  78.            
  79.         $con.Connect()
  80.         Write-Output $con
  81.     }
  82.     catch { $message = $_.Exception.GetBaseException().Message
  83.             write-error $message
  84.     }
  85.    
  86. } #Get-ServerConnection
  87.  
  88. #######################
  89. <#
  90. .SYNOPSIS
  91. Gets an SMO Server object.
  92. .DESCRIPTION
  93. The Get-SqlServer function  gets a SMO Server object for the specified SQL Server.
  94. .INPUTS
  95. None
  96.     You cannot pipe objects to Get-SqlServer
  97. .OUTPUTS
  98. Microsoft.SqlServer.Management.Smo.Server
  99.     Get-SqlServer returns a Microsoft.SqlServer.Management.Smo.Server object.
  100. .EXAMPLE
  101. Get-SqlServer "Z002\sql2K8"
  102. This command gets an SMO Server object for SQL Server Z002\SQL2K8.
  103. .EXAMPLE
  104. Get-SqlServer "Z002\sql2K8" "sa" "Passw0rd"
  105. This command gets a SMO Server object for SQL Server Z002\SQL2K8 using SQL authentication.
  106. .LINK
  107. Get-SqlServer
  108. #>
  109. function Get-SqlServer
  110. {
  111.     param(
  112.     [Parameter(Position=0, Mandatory=$true)] [string]$sqlserver,
  113.     [Parameter(Position=1, Mandatory=$false)] [string]$username,
  114.     [Parameter(Position=2, Mandatory=$false)] [string]$password,
  115.     [Parameter(Position=3, Mandatory=$false)] [string]$StatementTimeout=0
  116.     )
  117.     #When $sqlserver passed in from the SMO Name property, brackets
  118.     #are automatically inserted which then need to be removed
  119.     $sqlserver = $sqlserver -replace "\[|\]"
  120.  
  121.     Write-Verbose "Get-SqlServer $sqlserver"
  122.  
  123.     $con = Get-SqlConnection $sqlserver $Username $Password
  124.  
  125.     $server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $con
  126.     #Some operations might take longer than the default timeout of 600 seconnds (10 minutes). Set new default to unlimited
  127.     $server.ConnectionContext.StatementTimeout = $StatementTimeout
  128.     $server.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.StoredProcedure], "IsSystemObject")
  129.     $server.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.Table], "IsSystemObject")
  130.     $server.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.View], "IsSystemObject")
  131.     $server.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.UserDefinedFunction], "IsSystemObject")
  132.     #trap { "Check $SqlServer Name"; continue} $server.ConnectionContext.Connect()
  133.     Write-Output $server
  134.    
  135. } #Get-SqlServer
  136.  
  137. #######################
  138. <#
  139. .SYNOPSIS
  140. Gets an SMO Database object.
  141. .DESCRIPTION
  142. The Get-SqlDatabase function  gets an SMO Database object for the specified SQL Database or collection of Database objects.
  143. .INPUTS
  144. None
  145.     You cannot pipe objects to Get-SqlDatabase
  146. .OUTPUTS
  147. Microsoft.SqlServer.Management.Smo.Database
  148.     Get-SqlDatabase returns a Microsoft.SqlServer.Management.Smo.Database object.
  149. .EXAMPLE
  150. Get-SqlDatabase "Z002\sql2K8"
  151. This command gets a collection of SMO Database objects for SQL Server Z002\SQL2K8.
  152. .EXAMPLE
  153. Get-SqlDatabase $(Get-SqlServer "Z002\sql2K8" "sa" "Passw0rd") "pubs"
  154. This command gets a SMO Database object for SQL database pubs on the SQL Server Z002\SQL2K8 using SQL authentication.
  155. .LINK
  156. Get-SqlDatabase
  157. #>
  158. function Get-SqlDatabase
  159. {
  160.     param(
  161.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  162.     [Parameter(Position=1, Mandatory=$false)] [string]$dbname
  163.     )
  164.  
  165.     switch ($sqlserver.GetType().Name)
  166.     {
  167.         'String' { $server = Get-SqlServer $sqlserver }
  168.         'Server' { $server = $sqlserver }
  169.         default { throw 'Get-SqlDatabase:Param `$sqlserver must be a String or Server object.' }
  170.     }
  171.  
  172.     Write-Verbose "Get-SqlDatabase $($server.Name) $dbname"
  173.  
  174.     if ($dbname)
  175.     { if ($server.Databases.Contains("$dbname") -and $server.Databases[$dbname].IsAccessible)
  176.         {$server.Databases[$dbname]}
  177.       else
  178.         {throw "Database $dname does not exist or is not accessible."}
  179.     }
  180.     else
  181.     { $server.Databases | where {$_.IsAccessible -eq $true} }
  182.  
  183. } # Get-SqlDatabase
  184.  
  185. #######################
  186. <#
  187. .SYNOPSIS
  188. Executes a query and returns an array of System.Data.DataRow.
  189. .DESCRIPTION
  190. The Get-SqlData function executes a query and returns an array of System.Data.DataRow.
  191. .INPUTS
  192. None
  193.     You cannot pipe objects to Get-SqlData
  194. .OUTPUTS
  195. System.Data.DataRow
  196.     Get-SqlData returns an array of System.Data.DataRow.
  197. .EXAMPLE
  198. Get-SqlData "Z002\sql2K8" "pubs" "select * from authors"
  199. This command executes the specified SQL query using Windows authentication.
  200. .EXAMPLE
  201. $server = Get-SqlServer "Z002\sql2K8" "sa" "Passw0rd"
  202. Get-SqlData $server "pubs" "select * from authors"
  203. This command executes the specified SQL query using SQL authentication.
  204. .LINK
  205. Get-SqlData
  206. #>
  207. function Get-SqlData
  208. {
  209.     param(
  210.     [Parameter(Position=0, Mandatory=$false)] $sqlserver,
  211.     [Parameter(Position=1, Mandatory=$true)] $dbname,
  212.     [Parameter(Position=2, Mandatory=$true)] [string]$qry
  213.     )
  214.  
  215.     switch ($dbname.GetType().Name)
  216.     {
  217.         'String' { $database = Get-SqlDatabase $sqlserver $dbname }
  218.         'Database' { $database = $dbname }
  219.         default { throw "Get-SqlData:Param '`$dbname' must be a String or Database object." }
  220.     }
  221.  
  222.     #Write-Verbose "Get-SqlData $($database.Parent.Name) $($database.Name) $qry"
  223.     Write-Verbose "Get-SqlData $($database.Parent.Name) $($database.Name)"
  224.  
  225.     $ds = $database.ExecuteWithResults("$qry")
  226.     $ds.Tables | foreach { $_.Rows}    
  227.  
  228. }# Get-SqlData
  229.  
  230. #######################
  231. <#
  232. .SYNOPSIS
  233.  Executes a query that does not return a result set.
  234. .DESCRIPTION
  235. The Set-SqlData function executes a query that does not return a result set.
  236. .INPUTS
  237. None
  238.     You cannot pipe objects to Set-SqlData
  239. .OUTPUTS
  240. None
  241.     Set-SqlData does not produce any output.
  242. .EXAMPLE
  243. Set-SqlData "Z002\sql2K8" "pubs" "Update authors set au_lname = 'Brown' WHERE au_lname = 'White'"
  244. This command executes the specified SQL query using Windows authentication.
  245. .EXAMPLE
  246. $server = Set-SqlServer "Z002\sql2K8" "sa" "Passw0rd"
  247. Set-SqlData $server "pubs" "Update authors set au_lname = 'Brown' WHERE au_lname = 'White'"
  248. This command executes the specified SQL query using SQL authentication.
  249. .LINK
  250. Set-SqlData
  251. #>
  252. function Set-SqlData
  253. {
  254.     param(
  255.     [Parameter(Position=0, Mandatory=$false)] $sqlserver,
  256.     [Parameter(Position=1, Mandatory=$true)] [string]$dbname,
  257.     [Parameter(Position=2, Mandatory=$true)] [string]$qry
  258.     )
  259.  
  260.     switch ($dbname.GetType().Name)
  261.     {
  262.         'String' { $database = Get-SqlDatabase $sqlserver $dbname }
  263.         'Database' { $database = $dbname }
  264.         default { throw "Set-SqlData:Param '`$dbname' must be a String or Database object." }
  265.     }
  266.    
  267.     #Write-Verbose "Set-SqlData $($database.Parent.Name) $($database.Name) $qry"
  268.     Write-Verbose "Set-SqlData $($database.Parent.Name) $($database.Name)"
  269.    
  270.     $database.ExecuteNonQuery("$qry")
  271.  
  272. }# Set-SqlData
  273.  
  274. #######################
  275. <#
  276. .SYNOPSIS
  277. Adds a login to a SQL Server.
  278. .DESCRIPTION
  279. The Add-SqlLogin function adds a new login to the specified SQL server.
  280. .INPUTS
  281. None
  282.     You cannot pipe objects to Add-SqlLogin
  283. .OUTPUTS
  284. None
  285.     This function does not generate any output.
  286. .EXAMPLE
  287. Add-SqlLogin "Z002\sql2k8" "TestPSUnit" "SQLPSXTesting" 'SqlLogin'
  288. This command adds the TestPSUnit login to the Z002\sql2k8 server.
  289. .EXAMPLE
  290. $server = Get-SqlServer "Z002\sql2k8"
  291. Add-SqlLogin $server "TestPSUnit" "SQLPSXTesting" 'SqlLogin'
  292. This command adds the TestPSUnit login to the Z002\sql2k8 server.
  293. .LINK
  294. Add-SqlLogin
  295. #>
  296. function Add-SqlLogin
  297. {
  298.     param(
  299.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  300.     [Parameter(Position=1, Mandatory=$true)] [string]$name,
  301.     [Parameter(Position=2, Mandatory=$false)] [System.Security.SecureString]$password,
  302.     [Parameter(Position=3, Mandatory=$false)] [Microsoft.SqlServer.Management.Smo.LoginType]$logintype='WindowsUser',
  303.     [Parameter(Position=4, Mandatory=$false)] [string]$DefaultDatabase='master',
  304.     [Parameter(Position=5, Mandatory=$false)] [switch]$PasswordExpirationEnabled,
  305.     [Parameter(Position=6, Mandatory=$false)] [switch]$PasswordPolicyEnforced,
  306.     [Parameter(Position=7, Mandatory=$true)] [string]$ChangeOrder,
  307.     [Parameter(Position=8, Mandatory=$false)] [string]$Description
  308.     )
  309.  
  310.     $ErrorActionPreference = "Stop"
  311.  
  312.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  313.  
  314.     switch ($sqlserver.GetType().Name)
  315.     {
  316.         'String' { $server = Get-SqlServer $sqlserver }
  317.         'Server' { $server = $sqlserver }
  318.         default { throw 'Add-SqlLogin:Param `$sqlserver must be a String or Server object.' }
  319.     }
  320.  
  321.     Write-Verbose "Add-SqlLogin $($server.Name) $name"
  322.  
  323.     try {
  324.         $login = new-object ('Microsoft.SqlServer.Management.Smo.Login') $server, $name
  325.         $login.DefaultDatabase = $defaultDatabase
  326.         $login.LoginType = $logintype
  327.  
  328.         if ($logintype -eq 'SqlLogin')
  329.         {
  330.             if ($server.Information.Version.Major -ne '8')
  331.             {
  332.                 $login.PasswordExpirationEnabled = $($PasswordExpirationEnabled.IsPresent)
  333.                 $login.PasswordPolicyEnforced = $($PasswordPolicyEnforced.IsPresent)
  334.             }
  335.             $login.Create($password)
  336.         }
  337.         else {
  338.             $login.Create()
  339.         }
  340.  
  341.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  342.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  343.     }
  344.     catch {
  345.         $message = $_.Exception.GetBaseException().Message
  346.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  347.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  348.         Write-Error $message
  349.     }
  350.  
  351. } #Add-SqlLogin
  352.  
  353. #######################
  354. <#
  355. .SYNOPSIS
  356. Removes a login from a SQL Server.
  357. .DESCRIPTION
  358. The Remove-SqlLogin function removes a login from the specified SQL server.
  359. .INPUTS
  360. None
  361.     You cannot pipe objects to Remove-SqlLogin
  362. .OUTPUTS
  363. None
  364.     This function does not generate any output.
  365. .EXAMPLE
  366. Remove-SqlLogin "Z002\sql2k8" "TestPSUnit"
  367. This command removes the TestPSUnit login from the Z002\sql2k8 server.
  368. .EXAMPLE
  369. $server = Get-SqlServer "Z002\sql2k8"
  370. Remove-SqlLogin $server "TestPSUnit"
  371. This command removes the TestPSUnit login from the Z002\sql2k8 server.
  372. .LINK
  373. Remove-SqlLogin
  374. #>
  375. function Remove-SqlLogin
  376. {
  377.  
  378.     param(
  379.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  380.     [Parameter(Position=1, Mandatory=$true)] [string]$name,
  381.     [Parameter(Position=2, Mandatory=$true)] [string]$ChangeOrder,
  382.     [Parameter(Position=3, Mandatory=$false)] [string]$Description
  383.     )
  384.  
  385.     $ErrorActionPreference = "Stop"
  386.  
  387.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  388.  
  389.     switch ($sqlserver.GetType().Name)
  390.     {
  391.         'String' { $server = Get-SqlServer $sqlserver }
  392.         'Server' { $server = $sqlserver }
  393.         default { throw 'Remove-SqlLogin:Param `$sqlserver must be a String or Server object.' }
  394.     }
  395.  
  396.     Write-Verbose "Remove-SqlLogin $($server.Name) $name"
  397.  
  398.     try {
  399.         $login = $server.Logins["$name"]
  400.         if ($login) {
  401.             $login.Drop()
  402.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  403.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  404.         }
  405.         else
  406.         { throw "Login $name does not exist on server $($server.Name)." }
  407.     }
  408.     catch {
  409.         $message = $_.Exception.GetBaseException().Message
  410.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  411.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  412.         Write-Error $message
  413.     }
  414.  
  415. } #Remove-SqlLogin
  416.  
  417. #######################
  418. <#
  419. .SYNOPSIS
  420. Removes a login from a SQL Server.
  421. .DESCRIPTION
  422. The Set-SqlLogin function changes a SQL Login Password.
  423. .INPUTS
  424. None
  425.     You cannot pipe objects to Set-SqlLogin
  426. .OUTPUTS
  427. None
  428.     This function does not generate any output.
  429. .EXAMPLE
  430. $pwd_secure_string = read-host "Enter a Password:" -assecurestring
  431. Set-SqlLogin "Z002\sql2k8" "TestPSUnit" $pwd_secure_string
  432. This command sets the password for TestPSUnit login.
  433. .LINK
  434. Set-SqlLogin
  435. #>
  436. function Set-SqlLogin
  437. {
  438.  
  439.     param(
  440.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  441.     [Parameter(Position=1, Mandatory=$true)] [string]$name,
  442.     [Parameter(Position=2, Mandatory=$true)] [string]$password,
  443.     [Parameter(Position=3, Mandatory=$true)] [string]$ChangeOrder,
  444.     [Parameter(Position=4, Mandatory=$false)] [string]$Description
  445.     )
  446.  
  447.     $ErrorActionPreference = "Stop"
  448.  
  449.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  450.  
  451.     switch ($sqlserver.GetType().Name)
  452.     {
  453.         'String' { $server = Get-SqlServer $sqlserver }
  454.         'Server' { $server = $sqlserver }
  455.         default { throw 'Set-SqlLogin:Param `$sqlserver must be a String or Server object.' }
  456.     }
  457.  
  458.     Write-Verbose "Set-SqlLogin $($server.Name) $name"
  459.  
  460.     try {
  461.         $login = $server.Logins["$name"]
  462.         if ($login) {
  463.             if ($server.Information.Version.Major -eq '8') {
  464.                 $login.ChangePassword($password)
  465.             }
  466.             else {
  467.                 $login.ChangePassword($password,$true,$false)
  468.             }
  469.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | ?{$_.Key -ne 'password'} | %{"`n$($_.Key)=$($_.Value)"})
  470.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  471.         }
  472.         else
  473.         { throw "Login $name does not exist on server $($server.Name)." }
  474.     }
  475.     catch {
  476.         $message = $_.Exception.GetBaseException().Message
  477.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | ?{$_.Key -ne 'password'} | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  478.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  479.         Write-Error $message
  480.     }
  481.  
  482. } #Set-SqlLogin
  483.  
  484. #######################
  485. <#
  486. .SYNOPSIS
  487. Removes a login from a SQL Server.
  488. .DESCRIPTION
  489. The Set-SqlLoginDefaultDatabase function changes a SQL Login default database.
  490. .INPUTS
  491. None
  492.     You cannot pipe objects to Set-SqlLoginDefaultDatabase
  493. .OUTPUTS
  494. None
  495.     This function does not generate any output.
  496. .EXAMPLE
  497. Set-SqlLoginDefaultDatabase "Z002\sql2k8" "TestPSUnit" 'master'
  498. This command sets the default database for TestPSUnit login.
  499. .LINK
  500. Set-SqlLoginDefaultDatabase
  501. #>
  502. function Set-SqlLoginDefaultDatabase
  503. {
  504.  
  505.     param(
  506.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  507.     [Parameter(Position=1, Mandatory=$true)] [string]$name,
  508.     [Parameter(Position=2, Mandatory=$true)] [string]$DefaultDatabase,
  509.     [Parameter(Position=3, Mandatory=$true)] [string]$ChangeOrder,
  510.     [Parameter(Position=4, Mandatory=$false)] [string]$Description
  511.     )
  512.  
  513.     $ErrorActionPreference = "Stop"
  514.  
  515.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  516.  
  517.     switch ($sqlserver.GetType().Name)
  518.     {
  519.         'String' { $server = Get-SqlServer $sqlserver }
  520.         'Server' { $server = $sqlserver }
  521.         default { throw 'Set-SqlLoginDefaultDatabase:Param `$sqlserver must be a String or Server object.' }
  522.     }
  523.  
  524.     Write-Verbose "Set-SqlLoginDefaultDatabase $($server.Name) $name"
  525.  
  526.     try {
  527.         $login = $server.Logins["$name"]
  528.         if ($login) {
  529.             $login.DefaultDatabase = $DefaultDatabase
  530.             $login.Alter()
  531.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  532.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  533.         }
  534.         else
  535.         { throw "Login $name does not exist on server $($server.Name)." }
  536.     }
  537.     catch {
  538.         $message = $_.Exception.GetBaseException().Message
  539.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  540.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  541.         Write-Error $message
  542.     }
  543.  
  544. } #Set-SqlLoginDefaultDatabase
  545.  
  546. #######################
  547. <#
  548. .SYNOPSIS
  549. Removes a login from a SQL Server.
  550. .DESCRIPTION
  551. The Rename-SqlLogin function renames a SQL Login.
  552. .INPUTS
  553. None
  554.     You cannot pipe objects to Rename-SqlLogin
  555. .OUTPUTS
  556. None
  557.     This function does not generate any output.
  558. .EXAMPLE
  559. Rename-SqlLogin "Z002\sql2k8" "TestPSUnit" "CheckPSUnit"
  560. This command renames the login TestPSUnit.
  561. .LINK
  562. Rename-SqlLogin
  563. #>
  564. function Rename-SqlLogin
  565. {
  566.  
  567.     param(
  568.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  569.     [Parameter(Position=1, Mandatory=$true)] [string]$name,
  570.     [Parameter(Position=2, Mandatory=$true)] [string]$newname,
  571.     [Parameter(Position=3, Mandatory=$true)] [string]$ChangeOrder,
  572.     [Parameter(Position=4, Mandatory=$false)] [string]$Description
  573.     )
  574.  
  575.     $ErrorActionPreference = "Stop"
  576.  
  577.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  578.  
  579.     switch ($sqlserver.GetType().Name)
  580.     {
  581.         'String' { $server = Get-SqlServer $sqlserver }
  582.         'Server' { $server = $sqlserver }
  583.         default { throw 'Rename-SqlLogin:Param `$sqlserver must be a String or Server object.' }
  584.     }
  585.  
  586.     Write-Verbose "Rename-SqlLogin $($server.Name) $name $newname"
  587.  
  588.     try {
  589.         $login = $server.Logins["$name"]
  590.         if ($login) {
  591.             $login.Rename("$newName")
  592.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  593.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  594.         }
  595.         else
  596.         { throw "Login $name does not exist on server $($server.Name)." }
  597.     }
  598.     catch {
  599.         $message = $_.Exception.GetBaseException().Message
  600.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  601.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  602.         Write-Error $message
  603.     }
  604.  
  605. } #Rename-SqlLogin
  606.  
  607. #######################
  608. <#
  609. .SYNOPSIS
  610. Adds a new user to a database.
  611. .DESCRIPTION
  612. The Add-SqlUser function adds a new user to the specified database.
  613. .INPUTS
  614. None
  615.     You cannot pipe objects to Add-SqlUser
  616. .OUTPUTS
  617. None
  618.     This function does not generate any output.
  619. .EXAMPLE
  620. Add-SqlUser "Z002\sql2k8" "pubs" "TestPSUnit"
  621. This command adds the TestPSUnit login to the pubs database.
  622. .EXAMPLE
  623. $database = Get-SqlDatabase "Z002\sql2k8" "pubs"
  624. Add-SqlUser -dbname $database "TestPSUnit"
  625. This command adds the TestPSUnit login to the pubs database.
  626. .LINK
  627. Add-SqlUser
  628. #>
  629. function Add-SqlUser
  630. {
  631.     param(
  632.     [Parameter(Position=0, Mandatory=$false)] $sqlserver,
  633.     [Parameter(Position=1, Mandatory=$true)] $dbname,
  634.     [Parameter(Position=2, Mandatory=$true)] [string]$name,
  635.     [Parameter(Position=3, Mandatory=$false)] [string]$login=$name,
  636.     [Parameter(Position=4, Mandatory=$false)] [string]$defaultSchema='dbo',
  637.     [Parameter(Position=5, Mandatory=$true)] [string]$ChangeOrder,
  638.     [Parameter(Position=6, Mandatory=$false)] [string]$Description
  639.     )
  640.  
  641.     $ErrorActionPreference = "Stop"
  642.  
  643.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  644.  
  645.     switch ($dbname.GetType().Name)
  646.     {
  647.         'String' { $database = Get-SqlDatabase $sqlserver $dbname }
  648.         'Database' { $database = $dbname }
  649.         default { throw "Add-SqlUser:Param '`$dbname' must be a String or Database object." }
  650.     }
  651.  
  652.     Write-Verbose "Add-SqlUser $($database.Name) $name"
  653.  
  654.     try {
  655.         $user = new-object ('Microsoft.SqlServer.Management.Smo.User') $database, $name
  656.         $user.Login = $login
  657.         $loginObject = $sqlserver.Logins["$login"]
  658.         if ($database.parent.Information.Version.Major -ne '8' -and $loginObject.LoginType -ne "WindowsGroup" )
  659.         { $user.DefaultSchema = $defaultschema }
  660.  
  661.         $user.Create()
  662.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  663.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  664.     }
  665.     catch {
  666.         $message = $_.Exception.GetBaseException().Message
  667.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  668.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  669.         Write-Error $message
  670.     }
  671.  
  672. } #Add-SqlUser
  673.  
  674. #######################
  675. <#
  676. .SYNOPSIS
  677. Removes a user from a database.
  678. .DESCRIPTION
  679. The Remove-SqlUser function removes a user from the specified database.
  680. .INPUTS
  681. None
  682.     You cannot pipe objects to Remove-SqlUser
  683. .OUTPUTS
  684. None
  685.     This function does not generate any output.
  686. .EXAMPLE
  687. Remove-SqlUser "Z002\sql2k8" "pubs" "TestPSUnit"
  688. This command Removes the TestPSUnit user from the pubs database.
  689. .EXAMPLE
  690. $database = Get-SqlDatabase "Z002\sql2k8" "pubs"
  691. Remove-SqlUser -dbname $database "TestPSUnit"
  692. This command Removes the TestPSUnit user from the pubs database.
  693. .LINK
  694. Remove-SqlUser
  695. #>
  696. function Remove-SqlUser
  697. {
  698.     param(
  699.     [Parameter(Position=0, Mandatory=$false)] $sqlserver,
  700.     [Parameter(Position=1, Mandatory=$true)] $dbname,
  701.     [Parameter(Position=2, Mandatory=$true)] [string]$name,
  702.     [Parameter(Position=3, Mandatory=$true)] [string]$ChangeOrder,
  703.     [Parameter(Position=4, Mandatory=$false)] [string]$Description
  704.     )
  705.  
  706.     $ErrorActionPreference = "Stop"
  707.  
  708.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  709.  
  710.     switch ($dbname.GetType().Name)
  711.     {
  712.         'String' { $database = Get-SqlDatabase $sqlserver $dbname }
  713.         'Database' { $database = $dbname }
  714.         default { throw "Remove-SqlUser:Param '`$dbname' must be a String or Database object." }
  715.     }
  716.  
  717.     Write-Verbose "Remove-SqlUser $($database.Name) $name"
  718.  
  719.     try {
  720.         $user = $database.Users[$name]
  721.         if ($user) {
  722.             $user.Drop()
  723.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  724.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  725.         }
  726.         else
  727.         { throw "User $name does not exist in database $($database.Name)." }
  728.     }
  729.     catch {
  730.         $message = $_.Exception.GetBaseException().Message
  731.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  732.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  733.         Write-Error $message
  734.     }
  735.  
  736. } #Remove-SqlUser
  737.  
  738. #######################
  739. <#
  740. .SYNOPSIS
  741. Adds a login to a server role.
  742. .DESCRIPTION
  743. The Add-SqlServerRoleMember function adds a login to the specified server role.
  744. .INPUTS
  745. None
  746.     You cannot pipe objects to Add-SqlServerRoleMember
  747. .OUTPUTS
  748. None
  749.     This function does not generate any output.
  750. .EXAMPLE
  751. Add-SqlServerRoleMember "Z002\sql2k8" "TestPSUnit" "bulkadmin"
  752. This command adds the TestPSUnit login to the bulkadmin server role.
  753. .EXAMPLE
  754. $server = Get-SqlServer "Z002\sql2k8"
  755. Add-SqlServerRoleMember $server "TestPSUnit" "bulkadmin"
  756. This command adds the TestPSUnit login to the bulkadmin server role.
  757. .LINK
  758. Add-SqlServerRoleMember
  759. #>
  760. function Add-SqlServerRoleMember
  761. {
  762.     param(
  763.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  764.     [Parameter(Position=1, Mandatory=$true)] [string]$loginame,
  765.     [Parameter(Position=2, Mandatory=$true)] [string]$rolename,
  766.     [Parameter(Position=3, Mandatory=$true)] [string]$ChangeOrder,
  767.     [Parameter(Position=4, Mandatory=$false)] [string]$Description
  768.     )
  769.  
  770.     $ErrorActionPreference = "Stop"
  771.  
  772.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  773.  
  774.     switch ($sqlserver.GetType().Name)
  775.     {
  776.         'String' { $server = Get-SqlServer $sqlserver }
  777.         'Server' { $server = $sqlserver }
  778.         default { throw 'Add-SqlServerRoleMember:Param `$sqlserver must be a String or Server object.' }
  779.     }
  780.  
  781.     Write-Verbose "Add-SqlServerRoleMember $($server.Name) $name"
  782.  
  783.     if($server.Logins["$loginame"])
  784.     {
  785.         try {
  786.             $svrole = $server.Roles["$rolename"]
  787.             if ($svrole) {
  788.                 $svrole.AddMember($loginame)
  789.                 $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  790.                 write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  791.             }
  792.             else
  793.             { throw "ServerRole $rolename does not exist on server $($server.Name)." }
  794.         }
  795.         catch {
  796.             $message = $_.Exception.GetBaseException().Message
  797.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  798.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  799.             Write-Error $message
  800.             }
  801.     }
  802.     else
  803.     { throw "Login $loginame does not exist on server $($server.Name)." }
  804.  
  805. } #Add-SqlServerRoleMember
  806.  
  807. #######################
  808. <#
  809. .SYNOPSIS
  810. Removes a login from a server role.
  811. .DESCRIPTION
  812. The Remove-SqlServerRoleMember function removes a login from the specified server role.
  813. .INPUTS
  814. None
  815.     You cannot pipe objects to Remove-SqlServerRoleMember
  816. .OUTPUTS
  817. None
  818.     This function does not generate any output.
  819. .EXAMPLE
  820. Remove-SqlServerRoleMember "Z002\sql2k8" "TestPSUnit" "bulkadmin"
  821. This command Removes the TestPSUnit login from the bulkadmin server role.
  822. .EXAMPLE
  823. $server = Get-SqlServer "Z002\sql2k8"
  824. Remove-SqlServerRoleMember $server "TestPSUnit" "bulkadmin"
  825. This command Removes the TestPSUnit login from the bulkadmin server role.
  826. .LINK
  827. Remove-SqlServerRoleMember
  828. #>
  829. function Remove-SqlServerRoleMember
  830. {
  831.     param(
  832.     [Parameter(Position=0, Mandatory=$true)] $sqlserver,
  833.     [Parameter(Position=1, Mandatory=$true)] [string]$loginame,
  834.     [Parameter(Position=2, Mandatory=$true)] [string]$rolename,
  835.     [Parameter(Position=3, Mandatory=$true)] [string]$ChangeOrder,
  836.     [Parameter(Position=4, Mandatory=$false)] [string]$Description
  837.     )
  838.  
  839.     $ErrorActionPreference = "Stop"
  840.  
  841.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  842.  
  843.     switch ($sqlserver.GetType().Name)
  844.     {
  845.         'String' { $server = Get-SqlServer $sqlserver }
  846.         'Server' { $server = $sqlserver }
  847.         default { throw 'Remove-SqlServerRoleMember:Param `$sqlserver must be a String or Server object.' }
  848.     }
  849.  
  850.     Write-Verbose "Remove-SqlServerRoleMember $($server.Name) $name"
  851.  
  852.     if($server.Logins["$loginame"])
  853.     {
  854.         try {
  855.             $svrole = $server.Roles["$rolename"]
  856.             if ($svrole) {
  857.                 $svrole.DropMember($loginame)
  858.                 $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  859.                 write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  860.             }
  861.             else
  862.             { throw "ServerRole $rolename does not exist on server $($server.Name)." }
  863.         }
  864.     catch {
  865.         $message = $_.Exception.GetBaseException().Message
  866.         $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  867.         write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  868.         Write-Error $message
  869.         }
  870.     }
  871.     else
  872.     { throw "Login $loginame does not exist on server $($server.Name)." }
  873.  
  874. } #Remove-SqlServerRoleMember
  875.  
  876. #######################
  877. <#
  878. .SYNOPSIS
  879. Adds a user or role to a database role.
  880. .DESCRIPTION
  881. The Add-SqlDatabaseRoleMember function adds a user or role to the specified database role.
  882. .INPUTS
  883. None
  884.     You cannot pipe objects to Add-SqlDatabaseRoleMember
  885. .OUTPUTS
  886. None
  887.     This function does not generate any output.
  888. .EXAMPLE
  889. Add-SqlDatabaseRoleMember "Z002\sql2k8" "pubs" "TestPSUnit" "TestPSUnitDBRole"
  890. This command adds the TestUnit user to the TestPSUnitDBRole database role.
  891. .EXAMPLE
  892. $database = Get-SqlDatabase "Z002\sql2k8" "pubs"
  893. Add-SqlDatabaseRoleMember -dbname $database -name "TestPSUnit" -rolename "TestPSUnitDBRole"
  894. This command adds the TestUnit user to the TestPSUnitDBRole database role.
  895. .LINK
  896. Add-SqlDatabaseRoleMember
  897. #>
  898. function Add-SqlDatabaseRoleMember
  899. {
  900.     param(
  901.     [Parameter(Position=0, Mandatory=$false)] $sqlserver,
  902.     [Parameter(Position=1, Mandatory=$true)] $dbname,
  903.     [Parameter(Position=2, Mandatory=$true)] [string]$name,
  904.     [Parameter(Position=3, Mandatory=$true)] [string]$rolename,
  905.     [Parameter(Position=4, Mandatory=$true)] [string]$ChangeOrder,
  906.     [Parameter(Position=5, Mandatory=$false)] [string]$Description
  907.     )
  908.  
  909.     $ErrorActionPreference = "Stop"
  910.  
  911.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  912.  
  913.     switch ($dbname.GetType().Name)
  914.     {
  915.         'String' { $database = Get-SqlDatabase $sqlserver $dbname }
  916.         'Database' { $database = $dbname }
  917.         default { throw "Add-SqlDatabaseRoleMember:Param '`$dbname' must be a String or Database object." }
  918.     }
  919.  
  920.     Write-Verbose "Add-SqlDatabaseRoleMember $($database.Name) $name $rolename"
  921.  
  922.     if ($database.Users["$name"])
  923.     {
  924.  
  925.         try {
  926.             $role = $database.Roles["$rolename"]
  927.             if ($role) {
  928.                 $role.AddMember($name)
  929.                 $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  930.                 write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  931.             }
  932.             else
  933.             { throw "DatabaseRole $rolename does not exist in database $($database.Name)." }
  934.         }
  935.         catch {
  936.             $message = $_.Exception.GetBaseException().Message
  937.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  938.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  939.             Write-Error $message
  940.         }
  941.     }
  942.     else
  943.     { throw "User $name does not exist in database $($database.Name)." }
  944.  
  945. } #Add-SqlDatabaseRoleMember
  946.  
  947. #######################
  948. <#
  949. .SYNOPSIS
  950. Removes a user or role from a database role.
  951. .DESCRIPTION
  952. The Remove-SqlDatabaseRoleMember function removes a user or role from the specified database role.
  953. .INPUTS
  954. None
  955.     You cannot pipe objects to Remove-SqlDatabaseRoleMember
  956. .OUTPUTS
  957. None
  958.     This function does not generate any output.
  959. .EXAMPLE
  960. Remove-SqlDatabaseRoleMember "Z002\sql2k8" "pubs" "TestPSUnit" "TestPSUnitDBRole"
  961. This command removes the TestUnit user to the TestPSUnitDBRole database role.
  962. .EXAMPLE
  963. $database = Get-SqlDatabase "Z002\sql2k8" "pubs"
  964. Remove-SqlDatabaseRoleMember -dbname $database -name "TestPSUnit" -rolename "TestPSUnitDBRole"
  965. This command removes the TestUnit user to the TestPSUnitDBRole database role.
  966. .LINK
  967. Remove-SqlDatabaseRoleMember
  968. #>
  969. function Remove-SqlDatabaseRoleMember
  970. {
  971.     param(
  972.     [Parameter(Position=0, Mandatory=$false)] $sqlserver,
  973.     [Parameter(Position=1, Mandatory=$true)] $dbname,
  974.     [Parameter(Position=2, Mandatory=$true)] [string]$name,
  975.     [Parameter(Position=3, Mandatory=$true)] [string]$rolename,
  976.     [Parameter(Position=4, Mandatory=$true)] [string]$ChangeOrder,
  977.     [Parameter(Position=5, Mandatory=$false)] [string]$Description
  978.     )
  979.  
  980.     $ErrorActionPreference = "Stop"
  981.  
  982.     $PSUserName = $PSSenderInfo.UserInfo.Identity.Name
  983.  
  984.     switch ($dbname.GetType().Name)
  985.     {
  986.         'String' { $database = Get-SqlDatabase $sqlserver $dbname }
  987.         'Database' { $database = $dbname }
  988.         default { throw "Remove-SqlDatabaseRoleMember:Param '`$dbname' must be a String or Database object." }
  989.     }
  990.  
  991.     Write-Verbose "Remove-SqlDatabaseRoleMember $($database.Name) $name $rolename"
  992.  
  993.     if ($database.Users["$name"])
  994.     {
  995.         try {
  996.             $role = $database.Roles["$rolename"]
  997.             if ($role) {
  998.                 $role.DropMember($name)
  999.                 $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"})
  1000.                 write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage
  1001.             }
  1002.             else
  1003.             { throw "DatabaseRole $rolename does not exist in database $($database.Name)." }
  1004.         }
  1005.         catch {
  1006.             $message = $_.Exception.GetBaseException().Message
  1007.             $logmessage =  "PSUserName=$PSUserName" + $($psBoundParameters.GetEnumerator() | %{"`n$($_.Key)=$($_.Value)"}) + "`n$message"
  1008.             write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $logmessage -EntryType 'Error'
  1009.             Write-Error $message
  1010.         }
  1011.     }
  1012.     else
  1013.     { throw "User $name does not exist in database $($database.Name)." }
  1014.  
  1015. } #Remove-SqlDatabaseRoleMember
  1016.  
  1017. #######################
  1018. <#
  1019. .SYNOPSIS
  1020. Writes an entry to SqlProxy Log File.
  1021. .DESCRIPTION
  1022. The Write-SqlProxyLog function writes an entry to the SqlProxy Log File.
  1023. .INPUTS
  1024. None
  1025.     You cannot pipe objects to Write-SqlProxyLog
  1026. .OUTPUTS
  1027. None
  1028.     This function does not generate any output.
  1029. .EXAMPLE
  1030. $message =  "PSUserName=$PSUserName`n" + $($psBoundParameters.GetEnumerator() | %{"$($_.Key)=$($_.Value)"})
  1031. write-sqlproxylog -eventID $eventID."$($myinvocation.mycommand.name)" -message $message
  1032. This command writes a message to the SQLProxy Log
  1033. .NOTES
  1034. This must be run as administrator to create the new EventLog and EventLog Source!!!
  1035. New-EventLog -LogName SqlProxy -Source SqlProxy
  1036. .LINK
  1037. Write-SqlProxyLog
  1038. #>
  1039. function Write-SqlProxyLog
  1040. {
  1041.     param(
  1042.     [Parameter(Position=0, Mandatory=$true)] $EventID,
  1043.     [Parameter(Position=1, Mandatory=$true)] $Message,
  1044.     [Parameter(Position=2, Mandatory=$false)] $EntryType='SuccessAudit'
  1045.     )
  1046.  
  1047.     write-eventlog -logname SqlProxy -source SqlProxy -eventID $eventID -message $message -EntryType $EntryType
  1048.    
  1049. } #Write-SqlProxyLog

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me