Quantcast
Channel: PowerShell General
Viewing all 10624 articles
Browse latest View live

NA


Powershell Script to relocate User's Profile folder during Deprovision

$
0
0

Hi,

I'm trying to write a script which, upon execution, move a user's profile folder from it's original location (in the \\server1\profile$ folder) into a sub folder in that location (server1\profile$\deprovisioned). We use Quest Activ Roles so it will be run during the user deprovisioning process. It doesn't seem like a particularly tricky thing to do but i just can't seem to get it working. So far i've come up with:

 

function onDeprovision($Request)

{

if ($Request.class -eq "user")

{

$sAMAccountName = $Request.Get("sAMAccountName")

Move-Item "\\server1\profile$\$sAMAccountName" "\\server1\profile$\Moved"

}

}

Does this look about right? If it is correctly formed then i can start looking into other reasons why it might not be working. If it looks wrong then any help/correction would be much appreciated!

Thanks for the help

Can't get the Invoke-WmiMethod to work

$
0
0

The HP Display LiteSaver utility came with my monitor and stated:
HP Display LiteSaver installs a WMI Provider which creates a WMI object called Win32_DisplayPowerSaving in the root\CIMV2 namespace.
The WMI object has (among others) the following method: PowerOffNow (no parameters are given).

I'm completely new to WMI but wrote these two lines in PowerShell anyway:

PS C:\Users\allardb> $obj = Get-WmiObject -ComputerName AB -Namespace root/cimv2 -class win32_DisplayPowerSaving
PS C:\Users\allardb> $obj | Invoke-WmiMethod -Name PowerOffNow

No errors, no warnings, no result. Nothing happens.

What am I doing wrong?

How can I used Error Traping / Error Action with .NET type

$
0
0

Hello

I wounder if someone can help me with the following question please.

If I do the following

[

 

System.IO.Directory]::EnumerateFiles("\\Server1\Directory1","*.ps1",AllDirectories)

I get a list of files under remote Servers Directory 1 share that are of type .ps1 OK simply enough. Now if I hit a directory/files where I do not have access it will throw an Access Denined exception and the stop there, i.e. not going on the list any futher files. So basically I want to be able to do -ErrorAction SilentlyContinue  for example

I have tried using invoke-command {.....} -ErrorAction SilentlyContinue

but not joy

Any suggestion please around this subject, do I need to add some C# code to my PowerShell script?

Thanks All

Ernie

ADBackup Bitlocker

$
0
0

hello all, 

 

i am doing the following:

Invoke-Command -ComputerName $computername -ScriptBlock { manage-bde -protectors -get e: }

to get the bitlocker information, output is:

 

Volume E: [xxx]

Alle sleutelbeveiligingen

 

    Numeriek wachtwoord:

      Id: {aaaaaaaa-bbbb-111C-2222-1234567890qw}

      Wachtwoord:

        111111-222222-222222-333333-444444-555555-666666-777777

 

    Externe sleutel:

      Id: {12345678-1234-1232-2345-12233455667ty}

      Bestandsnaam externe sleutel:

        11111111-2222-3333-4444-555555555555555

      Automatische ontgrendeling is ingeschakeld.

 

i need a regulare expression to get the ID in bold  the other one i dont want, can someone help me with this ? 

 

thanks..

Functions of Functions

$
0
0

Sorry my terminology may be off as I'm learning still!

I'm writing code where I am going to reuse chunks of code in my script so want to make them into functions.

For example I can now easily add labels onto my forms by adding a function of:

# This Function Generates a label
function Label ($text="Default Label Text", $position_vertical=10, $position_horizontal=10, $width=100, $height=20) {
 $objLabel = New-Object System.Windows.Forms.Label
 $objLabel.Text = "$text" + ":"
 $objLabel.Location = New-Object System.Drawing.Size($position_horizontal,$position_vertical)
 $objLabel.Size = New-Object System.Drawing.Size($width,$height)
 $Form.Controls.Add($objLabel)
}

Then I can call this code time and again with the line (for example):

#Add a First Name text box.
Label -text "First Name" -position_vertical 40

This works nicely with the parameters having defaults so you only have to declare none standard ones.

My question is how would I do the same for a text box (and other form elements)? I can see the problem being that I need to vary a variable name! I somehow need to collect the output of the text box. I suspect I need to reference some Controls?

I tried to get round this with:

# This Function Generates a Textbox
function Textbox ($name="Default_Textbox", $position_vertical=10, $position_horizontal=120, $width=100, $height=20) {
 $objTextBox_$name = New-Object System.Windows.Forms.TextBox
 $objTextBox_$name.Location = New-Object System.Drawing.Size($position_horizontal,$position_vertical)
 $objTextBox_$name.Size = New-Object System.Drawing.Size($width,$height)
 $Form.Controls.Add($objTextBox_$name)
}

...but that would be way too easy and is blatently wrong. Can someone point me in the right direction.

Powershell and .Net Framework

$
0
0

Hello everyone,

First of all i would like to thank you guys because it's been a while that i'm reading the daily tips that you send and i find it really great to keep my mind into Powershell.

I understand most of the script that i receive but i'm a little bit concerning about .Net Framework.
I see some scripts have some .Net Framework part/syntax ( i don't really how to call them )

Anyway , i would like to know if it is a pre-requisite to learn Powershell or to use Powershell in an advance way.
I learnt that WIM is filling a gap that .Net Framework doesn't cover.

So my questions are :

Do i have to learn .NEt Framework ? If yes do you have some ressources that you could suggest me

Is it ok to learn just Powershell and look into WMI to build some nice scripts or scripts that would do the same if i don't know .NET Framework ?

Thank you in advance for your answers.

P.S. (not powershell :-) ) / Sorry if my English is not perfect , i'm french so feel free to correct me.

PowerShell - Parameter Question for a Newbie

$
0
0

Hello,

 

I am going trough an online course which is teaching me how to handle files with PHP. The chapter I am in right now has introduced Windows Powershell as a way to list file permission settings.

 

The instructor is using the command 'ls -a' which displays the files and the permission settings. It works well for him. However, when I try it myself I get this error and do not understand why: Get-ChildItem : A parameter cannot be found that matches parameter 'a'.

I use windows XP and  Powershell 1.0.

I searched high and low for an hour on Google about that but cannot find anything useful.

Can someone concisely explain that to me or what I am doing wrong? I would like to go over that hump in order to resume with the chapter.

 

The sooner the better,

 

Thanks a bunch in advance.


How to update sql and add to sql

$
0
0

I have created a table in SQL with the following command:  (bear with me, I am a newbie and after reading powershell in a month of lunches was able to do the following command successfully   Thanks, but now, I am sooooooooooo stuck…and just need to be pointed in the right direction.

 

$date = get-date

$Data=Get-DhcpServerv4Lease -ScopeId 0.0.0.0 -ComputerName dhcp-ed | Select-Object -Property Updated,IPAddress,ScopID,AddressState,ClientID,ClientType,Description,DNSRegistration,DNSRR,HostName,LeaseExpiryTime,NapCapable,NapStatus,PolicyName,ProbationEnds,ServerIP

$connection = "Server=dashboard;Database=NetworkInventory;Trusted_Connection=True;"

Import-Module C:\Scripts\out-sql.psm1

$Data | out-sql -sqlserver "dashboard" -database "networkinventory" -table "DHCP2" -dropexisting $true

 

The property listed above named updated did not exist in the dhcpserverv4lease information.  I added it to populate with a date, which I have already done.

Now, I need to:

$Data =Get-DhcpServerv4Lease -ScopeId 0.0.0.0 -ComputerName dhcp-ed

Update the table If the ClientID exists in $Data

If not

Add to the table and populate the updated filed with that current date.

And I have to tell you, I loved, LOVED your book "Powershell 3 in a Month of Lunches"  It took me about 5 weeks to do.  Having no experience, other than reading your book,  I was at least able to figure out the above statement!  I have purchased the toolmaking book and wish you had a powershell & SQL book.  If you know of a good "beginner" book, please direct me.  Thanks again!

Converting PSCustomObject to String

$
0
0

Hello everyone and thank you for taking the time to read my post.   As my subject states, I'm looking to convert a PSCustomObject type to a String type.  At least, I think that is what I need to do.  Allow me to explain more about what I'm trying to accomplish.  I'm trying to learn more about how to use the ListView Class.  More information on the Class is available here:  http://msdn.microsoft.com/en-us/library/System.Windows.Forms.ListView.aspx

I have a form, that contains the ListView class.  I have three columns that I need to populate with data.  So far this is simple enough.  Here is the code that works to populate just one column:

$test = Get-ChildItem -Path 'HKLM:\SOFTWARE\test\test agent\myagent\2.0'`

| Get-ItemProperty | Where-Object {$_.'name' -and $_.hidden -eq '0'}`

| select -ExpandProperty 'Name'

When I run the above code, I get back the following results:

Agent 1

Agent 2

Agent 3

Agent 4

Agent 5

When I pipe $test to get-member it returns a system.string type.  It is my belief that because my results are returned as a system.string type, I can easily add the above code to my ListView class, and have the results nicely populated into column 1.

The problem occurs when I remove the select -expandproperty 'Name' portion of the code.  I do this because I need more than just Name returned.  I need Name, Install Location, and Version.  In order to return those values, my code now looks like this:

$test = Get-ChildItem -Path 'HKLM:\SOFTWARE\test\test agent\myagent\2.0'`

| Get-ItemProperty | Where-Object {$_.'name' -and $_.hidden -eq '0'}`

| select 'Name','Install Location','Version'

Now when I pipe $test to get-member, it returns as a PSCustomObject type.  I believe this is no good for ListView.  I just do not know how to add a PSCustomObject type to the ListView class.  Therefore, I'm assuming I need to convert or change the type to a string type.  My assumption could absolutely be wrong, and that is OK.  Please feel free to enlighten me :-)

Executing Powershell through Batch

$
0
0
I am new to powershell and wanted to automate the existing .ps1 script using the Batch file. I wanted to seek help for the execution of Powershell through Batch script as my existing script didn't work. Below is the batch script for your view.
My batch file is:
echo "Changing the execution policy"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command {Set-ExecutionPolicy} > C:\Temp\a.out

echo "Updating the Server details:"

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "D:\test\Bluebird_1.2.0_RTM\ReplaceTagsOnConfigFiles.ps1;REPLACE" > C:\Temp\b.out
The powershell program is attached herewith.
When I try to execute the above batch file, the log file is empty and no error is thrown. Could you please help me in this regard.
(Please visit the site to view this media)

Searching for SSN with powershell

$
0
0

Experts,

I'm very new to powershell and scripting.  I'm trying to write a script to search my network for PII; starting with SSN's.  I need it to tell me where the files are exactly and I also need to to searh .doc, .xls, etc.  Can you tell me where i'm going wrong.  Here is what I have been using.

 

# set file to be tested for, put everything after c:\
# "c:\users\default" is the example path
$filetofind = (Get-Childitem -rec -exclude *.exe, *.dll | ?{ findstr.exe /mprc:. $_.fullname } |select-string "[0-9]{3}[-| ][0-9]{2}[-| ][0-9]{4}")
# Hostnames TXT Location
$hostnamestxt = 'C:\users\michael.obrien\desktop\computers.txt'
#Destination file for Positive Machines
$Positivetxt = 'C:\users\michael.obrien\desktop\positive.txt'
#destination file for Negative Machines
$Negativetxt = 'C:\users\michael.obrien\desktop\negative.txt'

##############################################################
# Begin Executing Script - Do Not Edit Below This Line
##############################################################

$computers = get-content "$hostnamestxt"
write-host "__________________"
write-host "Scanning hostnames for SSN"
write-host "__________________"

foreach($computer in $computers)
{
ping -n 1 $computer >$null
if($lastexitcode -eq 0)
{
if (test-path "\\$computer\C$\$filetofind")
{
out-file -append "C:\users\michael.obrien\desktop\results.txt"
echo "$computer" | Out-File -Append "$positivetxt"
write-host "SSN Present on System"
}
else
{
echo "$computer" | Out-file -append "$negativetxt"
write-host "File Not found on $computer"
}
}}

 

Thanks,

 

Michael OBrien

Xenapp application

$
0
0

Can powershell show how many users are using applications in a xenapp farm

Like

Outlook 3

Excel 22

 

Function for CRC32.

$
0
0

Hi,

Could any one post a function which generates a Hexa Decimal CRC32 value for a file using powershell.

windows 2012 & PowerShell commands

$
0
0

I was told in June while waiting in line at TechEd that when you execute a command in windows 2012 it is stored in a PowerShell format somewhere in a directory.  Does anyone know where this folder and/or log is located?  That would be way great for a beginner like myself.

Thanks very much,


How to find nth word in a line or string?

$
0
0

I have bunch of log files and want to extract certain words out of them, so they can be formatted into a summarized table. They are all similar enough to be able to know which word in a line that I need extracted. For example I want the 5th word in the 1st line. I understand I can use [1] to get the line that I need, but not sure about the word. Thank you for any help given!

Accessing Oracle Database 11g with Powershell

$
0
0

Hi there

 

Has anyone any experience with accessing a oracle 11g database in powershell?

 

Is there a module which can be downloaded in order to access a oracle database.

 

I want to extract data from a oracle database.

 

thanks for any hints

quotes within quotes witha variable.

$
0
0

Hi all


I am having an issue where i need to add " within an output this is due to spaces in the names. If i do not add the " i get what i need but since there are spaces in the names this will fail here is the code. the $fold is a folder structure within a share we have. $itm is the names of the folders. i am creating a bat file to copy each folder individually from source to destination.

$Fold = get-content c:\scripts\folders.txt
foreach ($Itm in $fold)
{
Write-output "start robocopy "F:\Information Technology\$itm" "J:\Information Technology" /MIR /COPYALL /ZB /W:3 /R:3 /TEE /LOG:C:\robocopy-f-to-j_$itm.log" | out-file c:\scripts\robo\$itm.bat
}

I know there is a way with "test '"text here'" text "           but how do you do

  "text '"some text $variable'" some more text"

PowerShell v3.0 HTML Parsing

$
0
0

I'm trying to build a new object with properties from this webpage:
http://us.battle.net/sc2/en/game/unit/
(Heart of the Swarm)

Ultimately I would like something like the following for every unit:

unit           strong-against                weak-against                     cost          time
--------        ----------------------               -------------------------               -------         ----
colossus   marine,zergling,zealot     viking,corruptor,immortal     300,200     75


I could maybe even add more properties once I get the basic idea. This code is what I have so far, but it seems that I have an array within the noteproperty "onclick" that I'm having a problem with. I'm unsure how to add each individual unit as a property (and not an array for one property).

#get main page
$url_main = 'http://us.battle.net/sc2/en/game/unit/'
If (!$main) {$main = Invoke-WebRequest $url_main}

#create new object with list of units
$units = $main.AllElements `
| Where-Object -Property class -Match "button\-rollover" `
| Select-Object -Property "onclick"

#clean up array
foreach ($unit in $units) {
    $unit = $unit -replace "@{onclick=Core.goTo\(\'" , ""
    $unit = $unit -replace "\'\)}" , ""
    $unit
}


thanks,

Ken

Update: This seems to be closer to what I'm looking for, but still not able to put it all into the above format.


#get main page
$url = 'http://us.battle.net/sc2/en/game/unit/'
If (!$result) {$result = Invoke-WebRequest $url}

#create new object with list of units
$result.AllElements `
| Where-Object -Property class -Match "button\-rollover" `
| Select-Object -ExpandProperty innerText

#unit,mineral,vespene,supply,buildtime,producer,life,shields,energy,armor

Help with getting date of last Windows Update from a list of several computers?

$
0
0

I have a txt file with a list of a jillion or so computers. What I'm trying to do is simply get the date of the last time Windows Update was run on each machine, so 1 line per machine.

I started out with this, which of course didn't work:

$computers=Get-Content-path allDesktops.txt

Get-hotfix-ComputerName$computers|sortinstalledon-desc|selectinstalledon-first1

Instead of getting 1 entry per computername, I'm only getting 1 entry altogether. How can I limit it so I get the last installedon date for each machine?

Thanks! Powershell is great, I'm way behind the curve just now trying to get into it :)

-Mike

Viewing all 10624 articles
Browse latest View live