Quantcast
Channel: Windows PowerShell Forum
Viewing all 2562 articles
Browse latest View live

Extracting Event Logs into a CSV file/ txt file or to html page

$
0
0
Hi all,

I am working on a Powershell Script, to extract event logs to a txt file, csv file or even a html page,
although i have a few problems, and i am new to powershell and need to understand some basics aswell.

Requirements -

To exract event id's from multiple servers which read servers from a text file, although how should you list the servers in a text file, the format?

and can someone help me save a log file to a location on the c drive

i have got as far as this below -

Get-eventlog system -newest 30 ¦ where-object {$_.eventid =eq 7036}

this was just trying out powershell which worked but i have tried so many commands and I have not got anywhere.

if anyone can help and have a script i would be most grateful and guide me to get this up and running. thanks in advance

How can I put the day of the Month into a variable?

$
0
0

Need to verify file is created daily.

File has the following format: producxx.csv

xx represents days of the month (0-31)

I basically just need to verify system creates it. If not created, then send an email notification.

I am using get-date -format d to get the date. I just don’t know how I can use days of the month to verify this.


Maximo Vizcaino

Power Shell is missing in my pc

$
0
0
Hi everyone, I downloaded this package: Windows6.0-KB968930-x86.msu and running in my pc. Installation completed without error but know I don't see power shell 2.0 ( neither power shell 1.0). Do you know why? Thanks 

list of all user who have logged onto a 2003 server, known or unknown

$
0
0
I really need to get a list of all users who have ever logged onto a server. Known or unknown... any advice would be appreciated.

check if the file does not exist for the current date. Check of the file is done once day mon-fri expects weekends and US Holidays

$
0
0

Hi everybody,

Need to pick your bring with this ps script.I need to check if the file does not exist for the current date. Format of the filename is mthfpcxx.csv where xx represents day of the month. Check of the file is done once day mon-fri expects weekends and US Holidays.

I managed to compile (csv file) the dates of all US holidays from 01/01/2013 to 12/25/2020


Maximo Vizcaino

get-adcomputers

$
0
0

I'm trying to build a script that pulls a list of active computer accounts from each of my 3 domains. I've built a shell command that will pull the list from AD and test-connection using the $_.dnshostname attribute. The idea here is to make sure the computer accounts are still valid (i know about lastlogon attribute, forget about that for now). This works great, I can output to a file and hand it off to my boss. Now I need to create a script that will do all 3 domains, and create either a single file or 3 files,  using the output from each of the three commands:

##

get-ADcomputer -filter * -searchbase "DC=any,DC=com" -server "any_domaincontroller" | forEach-object {test-connection -CN $_.dnshostname -count 1 -buffersize 16} | format-table address,ipv4address | out-file ".\domain"

##

I know there's a ton of ways to do this, but I thought I would post first before I start building lest I miss something incredibly obvious. Ideally i would do a convertto-html for all the output and dump it to IIS on a nightly basis for my boss, but at this point I would welcome suggestions on how to create a single script that runs all 3 commands, and cats to a single file. Maybe the easiest thing is to create 3 scheduled tasks? (one for each domain)

-josh

How can I change input values in a form while output is updating in the same form?

$
0
0

In the WorkDayTimer PS script below, I can enter the starting values, and when I click the Start Timer button, it runs fine for a few seconds, but then it stops. Also, once I start it, I can't change the input values on the form and then restart the timer because it will not accecpt any new input at all. I want the timer to continue to run while I'm entering new values, and then I want it to stop when I click the Start Timer button again, which should reset the timer and start it over with the new values I entered.

I'm sure it is stuck in an endless loop, and that it won't give the focus back to the form while looping, but I don't know how I should change it to fix this. I'm a novice, so I'm hoping there is an easy way to do this. Any help will be much appreciated.

# WorkDayTimer.ps1 

#Generated Form Function 
function GenerateForm { 

#region Import the Assemblies 
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null 
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null 
#endregion 
#region Generated Form Objects 
$form1 = New-Object System.Windows.Forms.Form 
$label4 = New-Object System.Windows.Forms.Label 
$tb_TimeRemaining = New-Object System.Windows.Forms.TextBox 
$button1 = New-Object System.Windows.Forms.Button 
$tb_MinutesForLunch = New-Object System.Windows.Forms.TextBox 
$tb_MinutesToWork = New-Object System.Windows.Forms.TextBox 
$tb_HoursToWork = New-Object System.Windows.Forms.TextBox 
$label3 = New-Object System.Windows.Forms.Label 
$label2 = New-Object System.Windows.Forms.Label 
$label1 = New-Object System.Windows.Forms.Label 
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState 
#endregion Generated Form Objects 
#---------------------------------------------- 
#Generated Event Script Blocks 
#---------------------------------------------- 
#Provide Custom Code for events specified in PrimalForms. 
$button1_OnClick=  
{ 
#TODO: Place custom script here 
  $startTime = get-date
  $HoursToWork = $tb_HoursToWork.text
  $MinutesToWork = $tb_MinutesToWork.text
  $MinutesForLunch = $tb_MinutesForLunch.text
  $endTime = $startTime.addHours($HoursToWork)
  $endTime = $endTime.addMinutes($MinutesToWork)
  $endTime = $endTime.addMinutes($MinutesForLunch)
  for(;;) {
  $timeSpan = new-timespan $(get-date) $endTime
  $tb_TimeRemaining.text = $([string]::Format("{0:d2}:{1:d2}:{2:d2}", $timeSpan.hours, $timeSpan.minutes, $timeSpan.seconds))
  $form1.Update()
  sleep 1
    }
} 
$handler_label1_Click=  
{ 
#TODO: Place custom script here 
} 
$OnLoadForm_StateCorrection= 
{#Correct the initial state of the form to prevent the .Net maximized form issue 
    $form1.WindowState = $InitialFormWindowState 
} 
#---------------------------------------------- 
#region Generated Form Code 
$form1.Text = 'Time left before end of workday' 
$form1.Name = 'form1' 
$form1.DataBindings.DefaultDataSourceUpdateMode = 0 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 300 
$System_Drawing_Size.Height = 250 
$form1.ClientSize = $System_Drawing_Size 
$label4.TabIndex = 8 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 23 
$label4.Size = $System_Drawing_Size 
$label4.Text = 'Time Remaining' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 149 
$System_Drawing_Point.Y = 43 
$label4.Location = $System_Drawing_Point 
$label4.DataBindings.DefaultDataSourceUpdateMode = 0 
$label4.Name = 'label4' 
$form1.Controls.Add($label4) 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 20 
$tb_TimeRemaining.Size = $System_Drawing_Size 
$tb_TimeRemaining.DataBindings.DefaultDataSourceUpdateMode = 0  
$tb_TimeRemaining.Name = 'tb_TimeRemaining' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 149 
$System_Drawing_Point.Y = 27 
$tb_TimeRemaining.Location = $System_Drawing_Point 
$tb_TimeRemaining.TabIndex = 4 
$form1.Controls.Add($tb_TimeRemaining) 
$button1.TabIndex = 6 
$button1.Name = 'button1' 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 75 
$System_Drawing_Size.Height = 23 
$button1.Size = $System_Drawing_Size 
$button1.UseVisualStyleBackColor = $True 
$button1.Text = 'Start Timer' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 13 
$System_Drawing_Point.Y = 27 
$button1.Location = $System_Drawing_Point 
$button1.DataBindings.DefaultDataSourceUpdateMode = 0 
$button1.add_Click($button1_OnClick) 
$form1.Controls.Add($button1) 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 20 
$tb_MinutesForLunch.Size = $System_Drawing_Size 
$tb_MinutesForLunch.DataBindings.DefaultDataSourceUpdateMode = 0 
$tb_MinutesForLunch.Text = '30' 
$tb_MinutesForLunch.Name = 'tb_MinutesForLunch' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 149 
$System_Drawing_Point.Y = 199 
$tb_MinutesForLunch.Location = $System_Drawing_Point 
$tb_MinutesForLunch.TabIndex = 3 
$form1.Controls.Add($tb_MinutesForLunch) 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 20 
$tb_MinutesToWork.Size = $System_Drawing_Size 
$tb_MinutesToWork.DataBindings.DefaultDataSourceUpdateMode = 0 
$tb_MinutesToWork.Text = '0' 
$tb_MinutesToWork.Name = 'tb_MinutesToWork' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 149 
$System_Drawing_Point.Y = 132 
$tb_MinutesToWork.Location = $System_Drawing_Point 
$tb_MinutesToWork.TabIndex = 2 
$form1.Controls.Add($tb_MinutesToWork) 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 20 
$tb_HoursToWork.Size = $System_Drawing_Size 
$tb_HoursToWork.DataBindings.DefaultDataSourceUpdateMode = 0 
$tb_HoursToWork.Text = '8' 
$tb_HoursToWork.Name = 'tb_HoursToWork' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 149 
$System_Drawing_Point.Y = 78 
$tb_HoursToWork.Location = $System_Drawing_Point 
$tb_HoursToWork.TabIndex = 1 
$form1.Controls.Add($tb_HoursToWork) 
$label3.TabIndex = 2 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 23 
$label3.Size = $System_Drawing_Size 
$label3.Text = 'Minutes for Lunch' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 12 
$System_Drawing_Point.Y = 196 
$label3.Location = $System_Drawing_Point 
$label3.DataBindings.DefaultDataSourceUpdateMode = 0 
$label3.Name = 'label3' 
$form1.Controls.Add($label3) 
$label2.TabIndex = 1 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 23 
$label2.Size = $System_Drawing_Size 
$label2.Text = 'Minutes to Work' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 13 
$System_Drawing_Point.Y = 135 
$label2.Location = $System_Drawing_Point 
$label2.DataBindings.DefaultDataSourceUpdateMode = 0 
$label2.Name = 'label2' 
$form1.Controls.Add($label2) 
$label1.TabIndex = 0 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 100 
$System_Drawing_Size.Height = 23 
$label1.Size = $System_Drawing_Size 
$label1.Text = 'Hours to Work' 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 12 
$System_Drawing_Point.Y = 78 
$label1.Location = $System_Drawing_Point 
$label1.DataBindings.DefaultDataSourceUpdateMode = 0 
$label1.Name = 'label1' 
$label1.add_Click($handler_label1_Click) 
$form1.Controls.Add($label1) 
#endregion Generated Form Code 
#Save the initial state of the form 
$InitialFormWindowState = $form1.WindowState 
#Init the OnLoad event to correct the initial state of the form 
$form1.add_Load($OnLoadForm_StateCorrection) 
#Show the Form 
$form1.ShowDialog()| Out-Null 
} #End Function 
#Call the Function 
GenerateForm 

Format-Table turn columns into variables

$
0
0

I am using PowerShell to point at a SharePoint list, look through the items, find the items that meet a certain criteria, and output a few of the columns using Format-Table.  I assigned this table the $newusers variable, so when I type $newusers I get the following:

FirstLast     Status
-------------- -------
JohnDoe      New
JaneDow     New

(sorry, I can't get the above to look like a table)

I would now like to run another PowerShell command using those values and a foreach statement, but I need to turn the columns into variables.  I want to iterate through that table, and run a command for each row.  For example:

new-aduser -name ($first + " " + $last) -givenname $first -surname $last -notes $status

(don't worry about the accuracy of the above syntax, it is used as an example)

How can I accomplish this?




Powershell Try Catch

$
0
0

how do i export error in txt file

$env:path = Read-Host "Enter Location"
$getcontent = $env:path


try
{
Import-csv $getcontent |
ForEach-Object {
Get-WmiObject -ComputerName $_.ipaddress win32_computername -ErrorAction SilentlyContinue | Select username}

}
Catch [exception]
 {
  write g:\script\erroripaddress.txt
 }


Support@Mytechnet.me

Send mail with multiple attachments with authentication

$
0
0

Hello All,

I am running into trouble trying to automate emails that send with multiple attachments AND authenticates on port 587.

This is trying to take all files within the $CopyDir and attach them via email. However, it will only attach 1 file and send. It properly authenticates on port 587, which is what we use. This cannot be changed.

$CopyDir = "somelocationonserver\files\"

------

$EmailFrom = "sendmailfrom@nomail.com" 
$EmailTo = "sendmailto@nomail.com"
$EmailBody = "Body of email"
$EmailSubject = "My Subject"
$Username = "myemailcredentials"
$Password = "mypassword"

$CopyDir = "C:\Users\Me\Desktop\Reports\"

$Message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
$Logfiles = Get-Childitem $CopyDir | WHERE {-NOT $_.PSIsContainer} | foreach{$_.fullname}
$Attachment = New-Object Net.Mail.Attachment($Logfiles)
$Message.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient("mysmtpserver.smtp.com", 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$SMTPClient.Send($Message)

-------

I try to re-write this using the send-mailmessage cmdlet. Here I assume I can attach multiple files, but it won't authenticate because I can't figure out how to pass the port with this cmdlet. The attachments are piped into the send-mailmessage.

-----

Get-ChildItem $CopyDir | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} |
send-mailmessage -from $EmailFrom -to $EmailTo -subject $EmailSubject -priority High -dno onSuccess, onFailure UseSSL=true -smtpServer mysmtpserver.smtp.com

-----

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

----

I can't seem to meet in the middle between these two. Any advice would help great. Again, I am looking to send multiple attachments (from $CopyDir), through an email service that requires authentication via Port 587.


Powershell script to enable "Connect client printers at logon" and "Default to main client printer" in AD.

$
0
0

I've been searching around and haven't found a solution yet.

Is there a Powershell script that can manipulate the "Client devices" under the Environment tab for all AD users?

I'd like to enable "Connect client printers at logon" and "default to main client printer", and make sure "Connet client drives at logon" is disabled.

Creating a custom Array

$
0
0

Hi,

I want to create a array and it has to be a 2d array. But i don`t know how i can fix it in the format i wan`t.

i need a kind of output like this:

        A B C D E F G H I J

10

9

8

7

6

5

4

3

2

1

It is for a little game for school. So after creating the array i need to get a output in a table.

I created an array with the following, but the problem is a user needs to give a coordinate like 1,D. With my own array i can only give coordinate 1,4.

$Array = New-Object 'object[,]' 10,10

Foreach ($Element in $Array) {
    $Element = " "
    }

Can anyone help me please??

Thanks in Advance


Script for rotating multiple IP address every 30 minutes

$
0
0

We have one newsletter server which sends reports and ezines to the opt-in database of 4,75,000 users four times in a day. Our application is managing the mail merge and through put very well but the challenge is on mail delivery cluster. We have a cluster of 6 mail servers catering to this huge load. There is one master distribution server which we use to distribute the traffic to the 6 servers on round robin fashion therefore the load is balanced. The challenge is that these 6 mail servers if operating on one single IP address get into bulk precedence and so for that we have added 10 IP's to each windows server.

I need to rotate these static IP's on the server on hourly basis using either some script or software for which I seek experts help. it should rotate the IP's from 1-10 each on hourly basis.

Thank you in advance for kind assistance :)

retrieving dll name, version, size and path

$
0
0

What is the best or efficient way to retrieve Microsoft Security Update dll attributes such as name, version, size, and path.

For certain there are two way I know.

either use the Get-ChildItem or Get-ItemProperty diectly from the registry.

I will be creating a function which will allow me to pass the name of the dll and logical drive which I will like to start searching.

Also I will have a list of servers and i will like to remote to these machines....

What I am interesting is in the core logic of which techinique will be more efficient Get-ChildItem or Get-ItemProperty?

thanks.... any that might I have left such as when searching WIN side by side path which contain same dll but difference version. How to retrieve the latest version on machine which are 2005 and 2008 but it will not apply for 2003 and 2010 which dll resides on system32.

After laying out all this.... then I will need to consider when machines will contain 32 and 64 software install. 

Any suggestions will be appreciate!!


michael john ocasio


powershell script to pull SCSI / SAN information

$
0
0

I required a powershell script to pull SCSI / SAN information (LUN no, storage ID & other details) thru remotely, Is it possible?

Gana

 


Ganapathy

Using Test-Path to find folder within another folder.

$
0
0

Hello all,

I am creating folders based off of the date. I then check to see if that folder was created, and this is where I am running into an issue since the folder name will be unique each day, I just can't say to look for folder "x". I think I ran into a mental block.

Here is my code.

$DestDir = "C:\Users\Desktop\Folder"

$Date = (Get-Date).tostring("MM-dd-yyyy")

$FolderCheck = (Test-Path -Path $DestDir -PathType Container $Date)

The first line is the location of $DestDir.

The second line grabs the date and stores it in $Date.

The third line is where I have the issue. 

I am looking for a folder under $DestDir that has the name of the current $Date.

The error I am getting is this,

Test-Path : A positional parameter cannot be found that accepts argument '01-12-2013

Any help is appreciated, thanks!


Determine if a web hosted file should be downloaded or not

$
0
0

Hi

Perhaps somebody may find this easy. I have a folder of files that are periodically downloaded from a website. This is not a lot of traffic as they are small files, but there are lots and this takes time. Is there a way I can add a loop that checks the file size in bytes on the hosted file compared to the local file and only download if there is a difference?

This is not a bullet proof way of checking if the two are the same, but it would eliminate 95% of downloads.

Usually I use Powershell as such for a much bigger loop:

	$clnt = new-object System.Net.WebClient
	$url = www.somesite.com/15415499.jpg
	$file = c:\temp\15415499.jpg
	$clnt.DownloadFile($url,$file)

Can I somehow get an 'if' in there that only downloads the $url that is different to $file? Note , $file may not exist if $url is a new file.

Many thanks

Gund

Power ping and get computer, username

$
0
0

Dear Exprt,

I am not getting result, can u check below code whats wrong in


function Ping-Host {
BEGIN {}
PROCESS {


$results = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = '$computer'"
$wmiobject = Get-WmiObject -ComputerName $computer win32_ComputerSystem

if ($results.StatusCode -eq 0) { 

 $wmiobject.name + $wmiobject.username

#Write-Host "$computer. is Pingable"
} else {

Write-Host "$computer is not Pingable" -BackgroundColor red 
}
}
END {}

$computers = Get-Content g:\script\ipaddress.txt
foreach ($computer in $computers) {
if (Ping-Host $computer) {
}



Support@Mytechnet.me

script to search for IP addresses within files and registry

$
0
0

Hi everyone I am looking for a script perfer Windows PowerShell if possible to search Windows clients file system and registry for a hard coded IP Address and display the results showing location of file and IP address found.

I need to search for addresses on a particular subnet like 192.168.xxx.xxx etc

 in file types 

.ini

.txt

.config

.bat

any help much appreciated

How can I force a script to use Powershell 2 not 3?

$
0
0

Hi,

I'm very new to PS...From a sharepoint 2010 server with PS 2 and 3 installed I'm trying to backup sharepoint 2010 using:

Add-PSSnapin Microsoft.SharePoint.PowerShell 
Backup-SPFarm -Directory c:\backup -BackupMethod Full 

But as .net 4 and PS 3 are installed it will not run....if i manually change to use version 2 with:

powershell -version 2

and then run the backup it works....

BUT this needs to be automated - called from task scheduler so I tried this:

powershell -version 2
Add-PSSnapin Microsoft.SharePoint.PowerShell 
Backup-SPFarm -Directory c:\backup -BackupMethod Full 

It does not create a backup...


I can not uninstall PS 3 either as the KB is missing and the option in Server Manager is greyed out plus removing .net 4 is not really an option either...

Thanks.



Viewing all 2562 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>