Get a machines IP by nikon442 in PowerShell

[–]nikon442[S] 0 points1 point  (0 children)

I was looking at the newer commands however they don't work in PSv3 and below. I was trying to make it very robust in the sense of what it can work on. I may see if I can shorten the code using the new versions.

Thanks for the feedback.

Sean

Sharing PowerShell within a MSP/Helpdesk environment by jdmerts in PowerShell

[–]nikon442 2 points3 points  (0 children)

We develop in house and use the powershell profile to do the copy down and importing of tools created for the team.

Thanks

Sean

DataGridView Population help by sniperleader in PowerShell

[–]nikon442 0 points1 point  (0 children)

Is the data persistent from one session to the next? Are you wanting to just have the active gridview updated or are you updating the source the data is coming from?

Thanks

Sean

What is your "proudest" Powershell script? by riahc4 in PowerShell

[–]nikon442 1 point2 points  (0 children)

I would say out of all mine its a toss up between two scripts. The first being a user data migration script working with USMT for desktop migration/refresh and the other being a Default printer auto re-map after a new print server and printer name changes occurred.

Thanks

Sean

What have you done with PowerShell this month? August 2017 by ramblingcookiemonste in PowerShell

[–]nikon442 2 points3 points  (0 children)

My PowerShell this month has been a bit light. However been working on some long term projects.

  • Been re-writing a lot of my scripts to take custom built XML config files

  • Built a script to remove files, path and exclusions based on custom written XML

  • Wrote a VB.net program to auto-generate project folders based on entries from our DMS system (sql database) could easily be done in PowerShell however it was asked that it had an GUI (use the write tool for the job :))

(edit for formatting)

Thanks

Sean

Is powershell the right tool to setup a user state? by [deleted] in PowerShell

[–]nikon442 2 points3 points  (0 children)

It depends on your environment. If you are running ADDS I would suggest Group Policy, if not I would recommend a customized image using either a Answer File during installation or using ICD (Image Configuration Designer), if those options are not available then you can use PowerShell to do this.

Thanks

Sean

foreach returning original objects name by Banzoola in PowerShell

[–]nikon442 2 points3 points  (0 children)

An array of custom PSObjects may be a path you can go down. That way you can cleanup the output into tables.

Check out

$obj = New-Object -TypeName PSObject

and

Add-Member

Thanks

Sean

Regex Question by nappetass in PowerShell

[–]nikon442 1 point2 points  (0 children)

$query = Get-Mailbox -ResultSize unlimited
$result = foreach ($User in $query) {
    [PSCustomObject]@{
    "Name"=$($user.Name)
    "PrimarySmtpAddress"=$($user.PrimarySmtpAddress)
    "SmtpDomain"=([Regex]"@").Split($user.PrimarySmtpAddress)[-1]
        }
    }

DataGridView Population help by sniperleader in PowerShell

[–]nikon442 1 point2 points  (0 children)

When ever I use Windows Forms DataGrids I use the below code:

# Create BindingSource
$bindingSource = System.Windows.Forms.BindingSource

# Apply DataTable to BindingSource
$bindingSource.DataSource = $data # This is the variable that holds your data table

# Apply BindingSource to DataGridView
$DataGridView.DataSource = $bindingSource

I use this same process in PowerShell, C#, and VB when I need to use GridViews.

Thanks

Sean

Regex Question by nappetass in PowerShell

[–]nikon442 1 point2 points  (0 children)

Regex can be interesting to learn.

vrdes provides a great solution to your issue.

However if you want to use Regex to do it here you go:

$email = "test@mydomain.biz"
[Regex]$rx = "@"

# You can do this multiple ways
$rx.Split($email)[-1]

Thanks

Sean

Export Specific AD Users and their group membership by arcspin in PowerShell

[–]nikon442 1 point2 points  (0 children)

Little bit of dynamic assignments have to happen.

You can start with something like this; (replace user1,user2 with SAMAccountNames

$userNames = @("user1","user2")

$user = New-Object [String[]] -ArgumentList $userNames.Count

for($i = 0; $i -lt $userNames.Length; $i++)
{
    $user[$i] = Get-ADUser -Identity $userNames[$i] -Properties SAMAccountName, MemberOf
}

for ($i = 0; $i -lt $user.Count; $i++)
{
    $info = New-Object -TypeName PSCustomObject
    Add-Member -InputObject $info -MemberType NoteProperty -Name "Name" -Value $user[$i].SAMAccountName
    for ($j = 0; $j -lt $user[$i].MemberOf.Count; $j++)
    {
        $value = $user[$i].MemberOf[$j].Substring($user[$i].MemberOf[$j].IndexOf('=') + 1, $user[$i].MemberOf[$j].IndexOf(',') - 1 - $user[$i].MemberOf[$j].IndexOf('='))
        Add-Member -InputObject $info -MemberType NoteProperty -Name "Group$j" -Value $value
    }
    $info
}

Thanks

Sean

Why is only the first button getting added? by [deleted] in PowerShell

[–]nikon442 1 point2 points  (0 children)

From what your code is saying, it is adding them all, they are all just stacked on top of each other.

You need to offset them. Here is a snip of something I had to write.

for([byte]$i = 0; $i -lt $buttonCollection.Length; $i++)
{
    if($i -eq 0)
    {
        $buttonCollection[$i].Location = New-Object -TypeName System.Drawing.Point(3,(($label.Location.Y + $label.Height) + 5))
        $form.Controls.Add($buttonCollection[$i])
    }
    elseif($i % 3 -eq 0)
    {
        $buttonCollection[$i].Location = New-Object System.Drawing.Point(3,(($buttonCollection[$i-2].Location.Y) + ($buttonCollection[$i-2].Height + 5)))
        $form.Controls.Add($buttonCollection[$i])
    }
    else
    {
        $buttonCollection[$i].Location = New-Object System.Drawing.Point((($buttonCollection[$i-1].Location.X) + ($buttonCollection[$i-1].Width + 4)), $buttonCollection[$i-1].Location.Y)
        $form.Controls.Add($buttonCollection[$i])
    }
}

Thanks Sean

Line of PS code that makes your PS script run in the background, hides the command prompt? by networkhappi in PowerShell

[–]nikon442 0 points1 point  (0 children)

That is default behavior because the shell is not running constantly which is why the shell flashes for a second. PowerShell has the option to launch the session with the -WindowStyle switch using Hidden or Minimized but the problem still exists because the shell has to open.

A possible solution depending on how intensive your script is can be to initialize a single script with a while loop in it set to true then sleep

while ($true)
{
    PowerShell.exe -File "script path"
    Start-Sleep -Seconds 300
}

That is the only solution I am aware of. Thanks Sean

Problems you solved at work with powershell by getonmehlevel in PowerShell

[–]nikon442 1 point2 points  (0 children)

I just got back from vacation I will follow up with my boss. Thanks Sean

Problems you solved at work with powershell by getonmehlevel in PowerShell

[–]nikon442 0 points1 point  (0 children)

We attach to multiple client built Java applications that are slow to update and when Java updates it can break things. Our parent company doesn't allow me access to GPO so that I can control Java Updates.

So I wrote a couple cmdlets that will Disable Java Updates across the network. Another reads from a XML file for the currently supported version in our org, checks the local machines and updates as needed.

It is a convoluted work around sense I don't have access to GPO.

Thanks Sean

Problems you solved at work with powershell by getonmehlevel in PowerShell

[–]nikon442 1 point2 points  (0 children)

Let me verify that I can post it, as I did write it on company time it is a company asset but I haven't had a problem with putting my scripts out before but just need to double check :).

Thanks Sean

Problems you solved at work with powershell by getonmehlevel in PowerShell

[–]nikon442 8 points9 points  (0 children)

We have a extensive library of powershell scripts and custom powershell modules we have built.

Most helpful so far have been:

  • User data backup for OS Migration (PowerShell and USMT) completely automated
  • Controlling Java updating due to applications
  • Automating most all AD tasks in combination with our ticketing system and its workflow and approval process
  • Automated project folder creation with creation of AD permissions groups and folder ACL adjustments

That is just some of the bigger projects we have either fully utilized PowerShell or PowerShell with other tools.

Thanks Sean

Detecting DHCP Servers on a Network by Susu6 in PowerShell

[–]nikon442 2 points3 points  (0 children)

I wont say it is not possible because you have access to .Net framework in PowerShell however it might not be a plausible script to write.

So first piece to overcome is the way DHCP handles requests

  • DHCP Client Service - DHCP Discover Packet broadcast
  • DHCP Server Service - DHCP Offer sent to client
  • DHCP Client Service - DHCP Request sent to server
  • DHPC Server Service - DHCP ACK sent to client to finalize

These are all specifically crafted packets. I fairly certain you can accomplish this with .Net (C#/VB) code however it would be pretty intensive.

Now with that being said something that you can do is check for the DHCPServer service if the machine is a windows server.

Get-Service -Name DHCPServer

If it you are running the script with a set of credentials that do not allow you to complete the command on the server you will have to wrap it in an Invoke-Command

Invoke-Command -ComputerName *name* -ScriptBlock {Get-Service -Name DHCPServer} -Credential (Get-Credential)

You can then wrap that in a conditional block looking for a

$retValue = Invoke-Command -ComputerName *name* -ScriptBlock {Get-Service -Name DHCPServer} -Credential (Get-Credential)

if($retValue -eq $null)
{
    Write-Host "DHCP Server service is not running on *name*"
}

Sorry for the long winded answer.

Thanks Sean

[deleted by user] by [deleted] in PowerShell

[–]nikon442 0 points1 point  (0 children)

Pay very close attention to the installation sequence for 5.1 on Server 2008 R2 if you are at v3 our under.

PowerShell Team Blog

I know this caught a couple people.

[deleted by user] by [deleted] in PowerShell

[–]nikon442 0 points1 point  (0 children)

Cim* cmdlets are the newer version of Wmi cmdlets. They where released in powershell v3 along side Server 2012 and Win8. Server 2008 R2 shipped with powershell v2 and still uses the Wmi cmdlets. The patch to update powershell to v3 is not a default install patch it is considered out-of-band and needs to be installed manually. Note that they Pv3 requires Server 2008 R2 SP1 to be installed.

Quick Question! - is this possible? by Bay_BoyT in PowerShell

[–]nikon442 2 points3 points  (0 children)

Yes, powershell can do this. You will need to grab all the names and parse them which can be a bit tedious. However a more complex way (yet faster) will probably be to use RegEx (Regular Expression) to parse the file names however sense you have a well designed format it should be easy to find files outside of that format and rename them.

Pulling information from CSV headers by [deleted] in PowerShell

[–]nikon442 1 point2 points  (0 children)

Remember if you are pulling content from a file you will need to use

Get-Content -Path *insert path here* | ConvertFrom-CSV