Jump to content

randle

Members
  • Posts

    689
  • Joined

  • Last visited

Everything posted by randle

  1. Hi, I have a strange problem with Windows 7 computers I've setup as Xibo (Digital Signage) clients. When these computers are pending a reboot after installing Windows updates they cannot be contacted be it via ping, UltraVNC, browsing UNC etc. I'm still investigating so haven't exhausted all avenues yet but can't see anything useful in the event logs and Googling the right combination of terms in order to not return generic "Unable to communicate....." issues is proving interesting! I suppose initially I wanted to see if anyone had experienced anything similar before I continue!?
  2. Yes, also works a treat thanks. Great to see alternative ways for future scripts.
  3. Well that's just smashing thanks. I couldn't have done it without your help and appreciate all the time you've given. I've learnt a lot along the way too. Watch out PowerShell...... Final script if anyone's interested: #Shows folder browser dialog box and passes on selection Function Get-FolderName() { Add-Type -AssemblyName System.Windows.Forms $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{ SelectedPath = 'I:\Temp\Exam data files\’ ShowNewFolderButton = $false Description = "Select folder to upload/remove" } #If cancel is clicked the script will exit If ($FolderBrowser.ShowDialog() -eq "Cancel") {Exit} $FolderBrowser.SelectedPath } #end function Get-FolderName #Shows a file selector dialog box and passes on selection Function Get-FileName($initialDirectory) { [system.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.Title = "Select text file that contains list of users" $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = "Text Files (*.txt)| *.txt" #"All files (*.*)| *.*" $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename #If cancel is clicked the script will exit If ($OpenFileDialog.filename -eq "") {Exit} } #end function Get-FileName #Checks the user list against AD for invalid users and reports back then terminates the script Function Check-Users() { $InvalidUsers = 0 Get-Content $UserFile | Foreach {$User = Get-ADUser -LDAPFilter "(sAMAccountName=$_)" If($User -eq $Null) { Write-Host -ForegroundColor Red "User $_ does not exist in AD. Exiting script" $InvalidUsers +=1 } } If($InvalidUsers -gt 0) { Pause Exit } } #Choice menu for upload, remove or quit [int]$xMenuChoiceA = 0 while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 ){ Write-host "1. Upload" Write-host "2. Remove" Write-host "3. Quit" [int]$xMenuChoiceA = read-host "Do you want to upload a folder or remove one? Select 1 to 3..." } Switch( $xMenuChoiceA ){ 1{ #Select user list text file to upload folder to Write-Host "Select text file that contains list of users`n" $UserFile = Get-FileName -initialDirectory "I:\Temp\Exam data files" #Checks the user list against AD for invalid users and reports back then terminates the script Check-Users #Select the folder to upload Write-Host "`nSelect the folder to upload" $FolderName = Get-FolderName #Gets each user's homedirectory path and copies the above folder selection over to each of them Get-Content $UserFile | Foreach {$User = Get-ADUser $_ -Properties homedirectory Copy-item -path $FolderName -destination $User.homedirectory -force -recurse Write-Host -Foregroundcolor Cyan "Folder `"$(($FolderName.split('\'))[-1])`" uploaded to $($User.homedirectory)"} } 2{ #Select user list text file to upload folder to Write-Host "Select text file that contains list of users`n" $UserFile = Get-FileName -initialDirectory "I:\Temp\Exam data files" #Checks the user list against AD for invalid users and reports back then terminates the script Check-Users #Select the folder to Remove Write-Host "`nSelect the folder to remove" $FolderName = Get-FolderName #Gets each user's homedirectory path and removes the above folder selection from each of them Get-Content $UserFile | Foreach {$User = Get-ADUser $_ -Properties homedirectory <# The following can be used as an alternative to set the path of the folder to delete $UserPath = $User.HomeDirectory + "\" + ($FolderName.Split('\'))[-1] Remove-Item -path $UserPath -force -recurse #> Remove-Item -path "$($User.homedirectory)\$(($FolderName.split('\'))[-1])" -force -recurse Write-Host -Foregroundcolor Cyan "Folder `"$(($FolderName.split('\'))[-1])`" removed from $($User.homedirectory)"} } 3{Exit} default{Exit} } Pause
  4. No worries. I've not found much help on my searches in regards to this issue with the FolderBrowserDialog class. As I said, it's more of a niggle than an issue. I was getting a little confused with how to incorporate your AD check so had a dabble myself and came up with the following: Get-Content $UserFile | Foreach {$User = Get-ADUser -LDAPFilter "(sAMAccountName=$_)" If($User -eq $Null) { Write-Host -ForegroundColor Red "User $_ does not exist in AD. Exiting script" } Else {} } This is added just after the $Get-Filename function action and works fine, listing invalid users before going any further and giving the user a chance to amend the user list however, If I add 'Exit' after the Write-Host string this only outputs one invalid user rather than all. I've tried putting it in different places but doesn't like this. Any ideas?
  5. I didn't try the OpenFileDialog function but can see there is a property for filename so my mistake. This works fine for me thanks. I was having mixed results with the FolderBrowserDialog function but turns out it really didn't like the [void] prefix which when used as part of an if statement would make the script skip this function! I tried piping to Out-Null like the OpenFileDialog function which I know is the same thing but just a more PS way of doing it but it didn't like this either. Excluding this allows the script to run as intended so will leave it out for now unless you think it should be there? The FolderBrowserDialog window, although opens at the specified SelectedPath, doesn't jump to the folder meaning that I have to scroll down to this which isn't the end of the world but would be nice! I've found on my research that this is just the way the class works so not too much that can be done but also, would be nice if the SelectedPath folder was already expanded! Adding '\' to the end of the path has no effect so any ideas? I'll give the AD check a look now. Thanks as always.
  6. Just concentrating on the dialog functions for now. I already tried wrapping the first method in an if statement "If([void]$FolderBrowser.ShowDialog() -ne "1") {Exit}", which did quit the script when the cancel button was clicked but also quit the script when a folder was selected! Unfortunately there's no property called 'FileName' for this class so can't use '$FolderBrowser.FileName' (https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx). It seems that adding this If statement has just thrown it off and now isn't returning the selected path. I have tried adding the Else statement 'Else {Return $FolderBrowser.SelectedPath}' with this but results in the same outcome!
  7. I've now joined the two scripts together with a choice menu at the beginning to choose whether you want to upload or remove a directory. This is working fine but want the script to exit if cancel is chosen on either the folder browser or open file dialogs. I've added an if statement based on the $FolderBrowser.ShowDialog() & $OpenFileDialog.ShowDialog() properties which does work but now prompts for the folder/file twice! I'm not sure why it's prompting again from an If statement!? I've been researching on how to do this but doesn't appear to be much on the web about this! Also I want to output a user friendly message when a user in the list cannot be found in the domain. I take it I should be using a Try/Catch method to do this? Function Get-FolderName() { Add-Type -AssemblyName System.Windows.Forms $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{ SelectedPath = 'I:\Temp\Exam data files\’ ShowNewFolderButton = $false Description = "Select folder to upload/remove" } [void]$FolderBrowser.ShowDialog() $FolderBrowser.SelectedPath If ($FolderBrowser.ShowDialog() -eq 2) {Exit} } #end function Get-FolderName Function Get-FileName($initialDirectory) { [system.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.Title = "Select text file that contains list of users" $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = "Text Files (*.txt)| *.txt" #"All files (*.*)| *.*" $OpenFileDialog.ShowDialog() | Out-Null If ($OpenFileDialog.ShowDialog() -eq 2) {Exit} $OpenFileDialog.filename } #end function Get-FileName [int]$xMenuChoiceA = 0 while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 ){ Write-host "1. Upload" Write-host "2. Remove" Write-host "3. Quit" [int]$xMenuChoiceA = read-host "Do you want to upload a folder or remove one? Select 1 to 3..." } Switch( $xMenuChoiceA ){ 1{ #Select the folder to upload Write-Host "Select the folder to upload" $FolderName = Get-FolderName #Select user list text file to upload folder to Write-Host "Select text file that contains list of users" $UserFile = Get-FileName -initialDirectory "I:\Temp\Exam data files" Get-Content $UserFile | Foreach {$User = Get-ADUser $_ -Properties homedirectory Copy-item -path $FolderName -destination $User.homedirectory -force -recurse Write-Host -Foregroundcolor Cyan "File(s)/Folder(s) uploaded to $($User.homedirectory)"} } 2{ #Select the folder to Remove Write-Host "Select the folder to remove" $FolderName = Get-FolderName #Select user list text file to upload folder to Write-Host "Select text file that contains list of users" $UserFile = Get-FileName -initialDirectory "I:\Temp\Exam data files" Get-Content $UserFile | Foreach {$User = Get-ADUser $_ -Properties homedirectory <# The following can be used as an alternative to set the path of the folder to delete $UserPath = $User.HomeDirectory + "\" + ($FolderName.Split('\'))[-1] Remove-Item -path $UserPath -force -recurse #> Remove-Item -path "$($User.homedirectory)\$(($FolderName.split('\'))[-1])" -force -recurse Write-Host -Foregroundcolor Cyan "File(s)/Folder(s) removed from $($User.homedirectory)"} } 3{Exit} default{Exit} }
  8. Ah yes of course. Makes sense and is obvious when you explain it...!
  9. I've had some luck using the other method of setting a variable as you mentioned: $UserPath = $User.HomeDirectory + "\" + ($FolderName.Split('\'))[-1] I'd like to know why the other way didn't work but no worries if it's not obvious. I really appreciate all the help you've provided and now have a script that's tidy and does what I want. The only steps I really have left is error collection/behaviour on hitting cancel and presenting a user friendly message if a user on the list doesn't exist in the domain.
  10. We're getting somewhere now. I have the following now: This is now grabbing the correct folder name but not sure what it means by 'A positional parameter cannot be found that accepts argument....' I appreciate your time and help thanks
  11. Great stuff thanks however, I'm now getting the following:
  12. Ok, last bit of help required.....maybe I now want a script to delete the same folder uploaded in the first one so thought I could use pretty much the same process but with remove-item instead of copy-item. So far I have: Function Get-FolderName() { Add-Type -AssemblyName System.Windows.Forms $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{ SelectedPath = 'I:\Temp\Exam data files\’ ShowNewFolderButton = $false Description = "Select folder to upload to user's My Documents" } [void]$FolderBrowser.ShowDialog() $FolderBrowser.SelectedPath } #end function Get-FolderName Function Get-FileName($initialDirectory) { [system.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.Title = "Select text file that contains list of users" $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = "Text Files (*.txt)| *.txt" #"All files (*.*)| *.*" $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename } #end function Get-FileName #Select the folder to Delete $FolderName = Get-FolderName #Select user list text file to delete folder from $UserFile = Get-FileName -initialDirectory "I:\Temp\Exam data files" Get-Content $UserFile | Foreach {$User = Get-ADUser $_ -Properties homedirectory Remove-Item -path $User.homedirectory\$FolderName -force -recurse Write-Host -Foregroundcolor Cyan "File(s)/Folder(s) removed from $($User.homedirectory)" } Pause With the above I get the error: I was hoping that selecting the folder would simply set the folder name to the variable rather than the whole path but it looks like the $User.homedirectory is being ignored altogether in this instance. Any ideas?
  13. Fun indeed but good to know. Thanks for that. I ended up adding it into your code above to save repeating a process. Get-Content $UserFile | Foreach {$User = Get-ADUser $_ -Properties homedirectory Copy-item -path $FolderName -destination $User.homedirectory -force -recurse Write-Host -Foregroundcolor Cyan "File(s)/Folder(s) uploaded to $($User.homedirectory)" }
  14. Following on from this I want an output to show where it's uploaded files to. I have: Get-Content $UserFile | Foreach {Write-Host -Foregroundcolor Cyan "File(s)/Folder(s) uploaded to $_ homedrive"} This gives me the username but not the homedirectory path. I've tried using "$User", "$User.homedirectory" & "$_.homedirectory" in place of "$_" but this just gives me the Distinguished name of the user. Any ideas?
  15. Ah smashing. I was getting close after I posted but missing a few things. This works fine now thanks.
  16. Ah!! I was adding it at the end of the line of last command or before the "Write-Host....". Didn't realise it was meant to go before the actual output but makes sense now. Thanks for your help with this
  17. Get-Service -DisplayName RM* -ComputerName $Computername | sort-object status Write-Host -ForegroundColor Cyan "RM Service Host Service restarted on $Computername"
  18. Hi, I'm using the following code to grab the homedirectory path of users Get-Content $UserFile | Foreach {Get-ADUser $_ -Properties homedirectory | Copy-Item -path $FolderName -Force -Recurse Don't worry, I'm aware this is flawed at the Copy-Item section but not sure where to go next. Basically the $UserFile variable has been set from a standard .NET Framework class function where you can select a txt file that contains domain users, each on a separate line. I want this command to find/use the home directory of each user in the file and copy a folder ($FolderName set by another class function) to each home dir. I know I need to set the homedirectory before the copy-item as this is currently just showing this property but a little lost at the moment. Any help mucho appreciated.
  19. Thanks for that. I did see this syntax on my searches but wasn't sure it was what I wanted but does look to do the job thanks however, the write-host line I'm tying to manipulate follows a Get-Service cmdlet so need the blank line before the write-host command. Adding `n at the beginning or on a line above doesn't work and can't add this to the end of the Get-Service cmdlet either.
  20. Hi, A very simply answer I'm sure but can I find it anywhere?....... I want the console output of a PowerShell script to have a blank line between each command so not to look like one clump of text. Am I missing the obvious other than adding a line with just "write-host"? I know I can use different colours for text but would still prefer a space.
  21. Thanks for getting back to me. Managed to get to the bottom of this in the end. Test B&R Exchange and MS SQL server | view topic Turns out that Windows was pending a restart after installing Windows updates and this was the cause of the failure and error code! Not sure why this would affect running SB scripts but appears to be the case. I’ve run the job again and all has completed successfully.
  22. Any luck with this in the end? I'm getting the same error for DNS, DC & GC roles. The following article linked off another forum with this same error and suggests that this is related to using VMXNET3 NICs but isn't something I can change in my environment. MAC address changes using the VMXNET3 adapter
  23. Great thanks, works a treat!
  24. Hi, I'm using the following in a PowerShell script to answer a question [font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500]$WantCompression[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9]=[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff]Read-Host[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000]"Do you want to compress the files/folders? Y/N"[/color][/size][/font][/color][/size][/font][/color][/size][/font] I want to have a default response if enter is pressed if possible!?
  25. Ah good call. Thanks for that. Much tidier now
×
×
  • Create New...