Goal of code: Extracting certain object properties of servers. I obtain the list of these servers with the following code:

 $serverlist = (get-adcomputer -filter {dnshostname -like "*SQL*" -and operatingsystem -like "windows *server*"}).name

I then iterate through each name in the list with:

$serverlist | ForEach-Object {

  $s = new-object Microsoft.SqlServer.Management.SMO.Server($serverlist)

 $s | Format-List @{Name = "Server";e={$s.Name}},
  @{Name = "VersionString";e={$s.VersionString}},
  @{Name = "Product Level";e={$s.ProductLevel}},
  @{Name = "Collation";e={$s.Collation}}, 
  @{Name = "Credentials";e={$s.Credentials}}, 
  @{Name = "Clustered?";e={$s.IsClustered}}, 
  @{Name = "One Owner?";e={$s.IsSingleUser}}, 
  @{Name = "Root";e={$s.RootDirectory}} | Out-File -path -append

I receive the new-object overload error for "server" as many times as there are servers. I'm not really sure what would be causing this issue; the code worked fine when I hard-coded one computer name in for $serverlist. I.E.

$serverlist = @("Some-Server-01")

Once I used the list of servers as the input, I started getting this error. Help would be appreciated.

Further error info:

At C:\somepath
+   $s = new-object Microsoft.SqlServer.Management.SMO.Server($serverli ...
+ CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
1

There are 1 answers

2
AudioBubble On BEST ANSWER

You probably want to create a new Server object, based on the individual servers' names, not based on $ServerList, which is an array of AD computer objects. Remember, you're working inside of a foreach loop, which runs once for each item in the object array ($ServerList).

You can't create a SQL SMO Server object using an array of computer objects. You create one based on the name of an individual system.

$s = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server -ArgumentList $PSItem