Jump to content

Recommended Posts

Posted

Hi There

 

I've been trying to get this powershell script to work it should produce a windows with a list of printers, It's using the powershell plus kit from the MSDN site (also in the win 7 resource kit).

 

$DetectedErrorState="unknown","other","No Error","Low Paper","No Paper",`
"Low Toner","No Toner","Door OPen","Jammed","Offline","Service Requested",`
"output Bin Full"

$PrinterStatus="Other","Unknown","Idle","Printing","Warming Up","Stopped Printing","Offiline"

#function test{
#    $class = "win32_printer"
#    $printers = Get-WmiObject -Class $Class -ComputerName "" -Credential \
#    foreach($p in $printers){
#        output $p.Name, $DetectedErrorState[$p.DetectedErrorState], $PrinterStatus[$p.PrinterStatus]
#    }
#}


New-ListView -Width 350 -Height 350 -DataBinding @{
   ItemsSource = New-Binding -IsAsync -UpdateSourceTrigger PropertyChanged -Path Output
} -View {
   New-GridView -AllowsColumnReorder -Columns {
       New-GridViewColumn "Name" 
       }
} -DataContext {
   Get-PowerShellDataSource -Script {
       #Get-Process | ForEach-Object { $_ ; Start-Sleep -Milliseconds 25 }
       Get-WmiObject -Class $Class -ComputerName "\ |`
       ForEach-Object {
           $_.Name
       }
   }
} -On_Loaded {
   Register-PowerShellCommand -Run -In "0:0:15" -ScriptBlock {
       $window.Content.DataContext.Script = $window.Content.DataContext.Script
   }
} -asjob 

 

If you look in the -DataContext section the commented out line will work, but the four lines below it don't. Can anyone see whats going wrong?

 

Thanks

 

.Adam

Posted

Hello Everyone

 

Got this from technet

 

New-ListView -Width 350 -Height 350 -DataBinding @{
   ItemsSource = New-Binding -IsAsync -UpdateSourceTrigger PropertyChanged -Path Output
} -View {
   New-GridView -AllowsColumnReorder -Columns {
       New-GridViewColumn "Name" 
       New-GridViewColumn "PrinterStatus"
       }
} -DataContext {
   Get-PowerShellDataSource -Script {
$list = @()
$PrinterStatus="Other","Unknown","Idle","Printing","Warming Up","Stopped Printing","Offiline"


Get-WmiObject -Class Win32_Printer | 
ForEach-Object {
$list += New-Object Object | Add-Member NoteProperty Name $_.name -passthru | Add-member NoteProperty PrinterStatus $PrinterStatus[$_.PrinterStatus-1] -passthru
}
foreach ($item in $list)
{ 
$item
}


   }
} -On_Loaded {
   Register-PowerShellCommand -Run -In "0:0:15" -ScriptBlock {
       $window.Content.DataContext.Script = $window.Content.DataContext.Script
   }
} -asjob 

 

which works however if you cange the -script block to

 

   Get-PowerShellDataSource -Script {
$list = @()
$PrinterStatus="Other","Unknown","Idle","Printing","Warming Up","Stopped Printing","Offiline"

$password = (Read-Host -AsSecureString)
$user = New-Object Management.Automation.PSCredential ("olympus\ap.admin", $password)

Get-WmiObject -Class Win32_Printer -computername "shsg-d02-prt01" -Credential $User | 
ForEach-Object {
$list += New-Object Object | Add-Member NoteProperty Name $_.name -passthru | Add-member NoteProperty PrinterStatus $PrinterStatus[$_.PrinterStatus-1] -passthru
}
foreach ($item in $list)
{ 
$item
}


   }

 

to allow for a remote computer it doesn't work any more... if anyone has any ideas...

 

 

Thanks

 

.Adam

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...