Cannot bind argument to parameter 'Path' because it is null


hi,

issue power shell when executing powershell script ps window, folder script has access rights everyone

current error - cannot bind argument parameter 'path' because null

if can me decipher , tune script great. have open source script on web.

------------------------------

                                    

$xyzdatabase= "f:\indexfragmentation\xyz database.htm" 
$serverlist = "f:\indexfragmentation\xyz.txt"
$querytimeout = 0

new-item -itemtype file $xyzdatabase-force

function invoke-sqlcmd2 ($server,$database,$query)
{
$conn=new-object system.data.sqlclient.sqlconnection
$conn.connectionstring="server={0};database={1};integrated security=true" -f $server,$database
$conn.open()
$cmd=new-object system.data.sqlclient.sqlcommand($query,$conn)
$cmd.commandtimeout=$querytimeout
$ds=new-object system.data.dataset
$da=new-object system.data.sqlclient.sqldataadapter($cmd)
[void]$da.fill($ds)
$conn.close()
$ds.tables[0]
}


function writehtmlheader  
{  
param($filename)  
$date = ( get-date ).tostring('yyyy/mm/dd - hh:mm')
add-content $filename "<html>" 
add-content $filename "<head>" 
add-content $filename "<meta http-equiv='content-type' content='text/html; charset=iso-8859-1'>" 
add-content $filename '<title>xyz database database fragmentation</title>' 
add-content $filename '<style type="text/css">' 
add-content $filename  "<!--" 
add-content $filename  "td {" 
add-content $filename  "font-family: tahoma;" 
add-content $filename  "font-size: 11px;" 
add-content $filename  "border-" 
add-content $filename  "border-right: 1px solid #999999;" 
add-content $filename  "border-bottom: 1px solid #999999;" 
add-content $filename  "border-" 
add-content $filename  "padding-" 
add-content $filename  "padding-right: 0px;" 
add-content $filename  "padding-bottom: 0px;" 
add-content $filename  "padding-" 
add-content $filename  "}" 
add-content $filename  "body {" 
add-content $filename  "margin-" 
add-content $filename  "margin-" 
add-content $filename  "margin-right: 0px;" 
add-content $filename  "margin-bottom: 10px;" 
add-content $filename  "" 
add-content $filename  "table {" 
add-content $filename  "border: thin solid #000000;" 
add-content $filename  "}" 
add-content $filename  "-->" 
add-content $filename  "</style>" 
add-content $filename "</head>" 
add-content $filename "<body>" 
add-content $filename  "<table width='100%'>" 
add-content $filename  "<tr bgcolor='#cccccc'>" 
add-content $filename  "<td colspan='7' height='25' align='center'>" 
add-content $filename  "<font face='tahoma' color='#003399' size='4'><strong>xyz database database fragmentation - $date</strong></font>" 
add-content $filename  "</td>" 
add-content $filename  "</tr>" 
add-content $filename  "</table>" 
}  

function writetableheader  
{  

param($filename)  
add-content $filename "<tr bgcolor=#cccccc>" 
add-content $filename "<td width='20%' align='center'>server name</td>"
add-content $filename "<td width='20%' align='center'>database name</td>"
add-content $filename "<td width='20%' align='center'>table name</td>"
add-content $filename "<td width='20%' align='center'>index name</td>"
add-content $filename "<td width='20%' align='center'>average fragmentation</td>"
add-content $filename "<td width='20%' align='center'>page count</td>"
add-content $filename "</tr>" 
}  

function writehtmlfooter  
{  
param($filename)  
add-content $filename "</body>" 
add-content $filename "</html>" 
}  


function sendemail  
{ param($from,$to,$subject,$smtphost,$htmlfilename)  
$body = get-content $htmlfilename 
$smtp= new-object system.net.mail.smtpclient $smtphost 
$msg = new-object system.net.mail.mailmessage $from, $to, $subject, $body
$msg.isbodyhtml = $true 
$smtp.send($msg)  
}


function writeserviceinfo  
{  
param($filename,$machinename,$dbname,$tbname,$ixname,$dbfrg,$dbpc)
add-content $filename "<tr>" 
add-content $filename "<td>$machinename</td>" 
add-content $filename "<td align=center>$dbname</td>"
add-content $filename "<td align=center>$tbname</td>"
add-content $filename "<td align=center>$ixname</td>"
$frg = [math]::round($dbfrg,2)
if ($frg -gt "30"){
add-content $filename "<td bgcolor='#ff0000' align=center>$frg</td>"}
else {
  add-content $filename "<td bgcolor='#387c44' align=center>$frg</td>" }

if ($dbpc -gt "1000"){
add-content $filename "<td bgcolor='#ff0000' align=center>$dbpc</td>"}
else {
  add-content $filename "<td bgcolor='#387c44' align=center>$dbpc</td>" }

add-content $filename "</tr>"
}

# load smo assembly, , if we're running sql 2008 dlls load smoextended , sqlwmimanagement libraries
$v = [system.reflection.assembly]::loadwithpartialname( 'microsoft.sqlserver.smo')
if ((($v.fullname.split(','))[1].split('='))[1].split('.')[0] -ne '9') {
  [system.reflection.assembly]::loadwithpartialname('microsoft.sqlserver.smoextended') | out-null
  [system.reflection.assembly]::loadwithpartialname('microsoft.sqlserver.sqlwmimanagement') | out-null
  }

# handle errors occur
trap {
  # handle error
  $err = $_.exception
  write-host $err.message
  while( $err.innerexception ) {
$err = $err.innerexception
write-output $err.message
};
  # end script.
  break
  }


writehtmlheader $xyz database

foreach ($server in get-content $serverlist)
{
add-content $xyzdatabase"<table width='100%'><tbody>" 
add-content $xyzdatabase"<tr bgcolor='#cccccc'>" 
add-content $xyzdatabase"<td width='100%' align='center' colspan=6><font face='tahoma' color='#003399' size='2'><strong> $server </strong></font></td>" 
add-content $xyzdatabase"</tr>" 
writetableheader $xyz database

# connect specified instance
$s = new-object ('microsoft.sqlserver.management.smo.server') $server

# databases instance, , iterate through them
$dbs = $s.databases
foreach ($db in $dbs) {
 # check make sure database not system database, , accessible
 if ($db.issystemobject -ne $true -and $db.isaccessible -eq $true -and $db.name -like 'xyz') {
   # store database name reporting
   $dbname = $db.name
   $dbid = [string]$db.id

   $tbs = $db.tables
   foreach ($tb in $tbs) {
     # store table name reporting
     $tbname = $tb.name
     $tbid = [string]$tb.id

   $ixs = $tb.indexes
     foreach ($ix in $ixs) {
       # don't want process xml indexes
if ($ix.isxmlindex -eq $false) {
         # store index name reporting
         $ixname = $ix.name
         $ixid = [string]$ix.id

              # fragmentation , page count information 
          $q = @"
 select avg_fragmentation_in_percent, page_count
 from sys.dm_db_index_physical_stats($dbid, $tbid, $ixid, null, null)
"@ 
          $res = invoke-sqlcmd2 $server $dbname $q
          $frval = $res.avg_fragmentation_in_percent
          $pgcnt = $res.page_count  
          writeserviceinfo $xyzdatabase$server $dbname $tbname $ixname $frval $pgcnt 
        }
        }
      }

    }
  }
add-content $xyzdatabase"</table>" 
}

writehtmlfooter $xyz database
$date = ( get-date ).tostring('yyyy/mm/dd - hh:mm')
sendemail x.abc.com "x.y@abc.com" "xyz database database fragmentation level" x.x.com $xyz database

-----------------------------------

1. use code block when posting scripts.

2. on line receiving error? copied , pasted entire script, suppose try , figure out went wrong?


if find post has answered question, please mark answer. if find post helpful in anyway, please click vote helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

0x300000d errors in Microsoft Remote Desktop client