I am having an issue with a function of mine acception a parameter. If I copy/paste the function into the shell and run it then set the variable and call the function it works! But if I run the script the function doesnt accept the parameter. Below is the primary snippets, if you want the whole script let me know.
$user = 'someusername' #$computer = 'somecomputer' function PrinterData { param( [string]$computer, [string]$user ) $sqlserver = "CONFIGMGRSERVER" $sqldb = "CM_CAS" $sqlquery = " SELECT dbo.v_R_System.Name0, dbo.v_R_System.User_Name0, dbo.v_GS_PRINTER_DEVICE.Name0, dbo.v_GS_PRINTER_DEVICE.DeviceID0, dbo.v_GS_PRINTER_DEVICE.DriverName0, dbo.v_GS_PRINTER_DEVICE.PortName0 FROM dbo.v_R_System INNER JOIN dbo.v_GS_PRINTER_DEVICE ON dbo.v_R_System.ResourceID = dbo.v_GS_PRINTER_DEVICE.ResourceID where (PortName0 not like 'LPT%' and PortName0 not like '%HPFAX' and PortName0 not like '%rightfax\spooljob%' and PortName0 not like '%TechSmith%' and PortName0 not like '%pdf%' and PortName0 not like 'Microsoft%:' and PortName0 != 'nul:' and PortName0 != 'SHRFAX:' and PortName0 != 'XPSPort:' and PortName0 != 'CCMS_FAX' and PortName0 not like '%APDCPort%:%' and PortName0 not like '%COM%:%' and PortName0 not like 'c:\%' and PortName0 != 'FAX:' and PortName0 != 'File:' and PortName0 != 'CPW2:' and PortName0 != 'FXC:' and PortName0 not like 'iceport%' and PortName0 != 'NVK6:' and dbo.v_GS_PRINTER_DEVICE.Name0 not like '%pdf%') and (dbo.v_R_System.Name0 = '$computer' or dbo.v_R_System.User_Name0 = '$user')" $connection_string = "server=$sqlserver;database=$sqldb;Integrated Security=sspi" $sqlConnection = new-object System.Data.SqlClient.SqlConnection $connection_string $sqlConnection.Open() $adapter = new-object data.sqlclient.sqldataadapter($sqlquery, $sqlConnection) $set = new-object data.dataset $adapter.fill($set) | out-null $global:table = new-object data.datatable $global:table = $set.tables[0] write-host "--- Found the following printers to migrate ---" -ForegroundColor cyan $global:table }
function PrinterList { Param( $printerip ) write-host "Printerip: $printerip" $mysqlserver = "SERVER" $mysqldb = "printer_list" $mysqluser = "USER" $mysqlpassword = 'PASS' $mysqlconnectionstring = "Server=$mysqlserver; Database=$mysqldb; User=$mysqluser; Password=$mysqlpassword" $mysqlquery = " SELECT * FROM printers WHERE ip = '$printerip'" [void][System.Reflection.Assembly]::LoadFrom("D:\scripts\printerinstall\MySql.Data.dll") $connection = New-Object MySql.Data.MySqlClient.MySqlConnection $connection.ConnectionString = $mysqlconnectionString $connection.Open() $command = New-Object MySql.Data.MySqlClient.MySqlCommand($mysqlquery, $connection) $dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command) $mysqltable = New-Object System.Data.DataTable $recordCount = $dataAdapter.Fill($mysqltable) $global:mysqltable | select Model,IP,Building,Floor,Printer_ID }
$dataip = $table.PortName0 write-host "DataIP: $dataip" $driver = $table.DriverName0 write-host "Driver: $driver" $printer = printerlist -printerip $dataip $printer
I am not sure if its because of the type of data the first function returns or if there is something wrong with my printerlist function.
Any ideas?