Quantcast
Channel: PowerShell
Viewing all 15370 articles
Browse latest View live

Converting an xls file to csv using powershell via task scheduler fails

$
0
0

Years ago I wrote a powershell script to be run automatically every night to convert 2 files from .xls to .csv.

The server this ran on is being retired as it's a Windows 2008 server and I'm moving to a 2016 server for my automated tasks.

I have an error log function that I built (with help from existing sources) to capture any errors during processing and it emails me if the task fails with detailed output.

Since I've moved the script to the 2016 server, I've run into a situation where every time I attempt to run the script via task scheduler, it fails with the following error:

----

Error: An error has occurred [System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Excel cannot access the file 'C:\fbserver\FTP\CRM Membership.xls'. There are several possible reasons:

• The file name or path does not exist.
• The file is being...


Robocopy to array and dealing with apostrophe

$
0
0

So, my questions regarding spaces was solved via this question

https://community.spiceworks.com/topic/2183104-robocopy-to-array-and-dealing-with-spaces

Now, however, I'm finding apostrophe's in our directory structure. How can I change this line to deal with apostrophes?

Text
If ($_ -match "(?\d+)\s(?\S+\s\S+)\s+(?[\\\w\s]+)\\(?[\s\w]+\.\w+$)") 

Taken from greater code

Text
$ZeroList = Invoke-Expression -Command "Robocopy $SharePath NULL /XD $ExcludeDirs /XF $ExcludeFiles /L /S /NJH /NJS /BYTES /FP /NC /NDL /XJ /TS /R:0 /W:0" | ForEach { If ($_ -match "(?\d+)\s(?\S+\s\S+)\s+(?[\\\w\s]+)\\(?[\s\w]+\.\w+$)") #If ($_ -match "(?\d+)\s(?\S+\s\S+)\s+(?\S+)\\(?.*$)") { If ([int64]$Matches.Size -eq 0) { [PSCustomObject]@{ Folder = $Matches.Folder Name = $Matches.Name # Size = [int32]$Matches.Size #LastWriteTime = [datetime]$Matches.Date } } } }...

Need Script to Run on All AD Devices

$
0
0

I am trying to disable netbios on every computer in my Active Directory environment. I have a good command that will disable it when run locally on a computer. My issue is trying to remotely run that command on all computers. I enabled a GPO that allows for remote access, but the powershell command does not do what I am wanting it to do. When I run the command, it doesn't disable netbios, it doesn't do anything, it's as if the command wasn't run at all.

Can anyone see why this command will not run the same as if it would on a local computer? Or, any other way you would suggest running that command remotely on all computers? I don't get an error message when this is run, it actually completes without issue. But when I check the netbios setting on the remote computer, it's not changed.

Powershell
Import-ModuleActiveDirectory$OU=...

Powershell - Last password change

$
0
0

Hi There all

I am new to this whole Powershell stuff, so please bare with me.

I have been tasked to get a list of all users in our OU, and when their passwords were last changed. I thought this would be simple, but i am wrong. My results are so wonky. I get about 60 results (out of 700+) that tell me when the password was last changed, but the rest of the results are blank. I am very confused about where to go from here. Why are some of the results blank? At first i thought it was users who have disabled accounts, but it is not just them, it is most of the company along side this. There is no common denominator, it is not only admins who have their last changed passwords on there. Not all in the same groups, nothing is similar. It is very strange. The script is below. Hopefully someone can help:

get-aduser -filter * -properties...

Copy Item from Source to a different destination path

$
0
0

Hi All,

I need to copy some files and subfolders to a different destination path, see below for an example:

Source: C:\Customer
Destination J:\Customer

Source Folder example list to copy - C:\Customer\M\MORRI01
C:\Customer\M\MORRI02
C:\Customer\T\TEST02

Destination folder example path - J:\Customer\MORRI01
J:\Customer\MORRI02
J:\Customer\TEST02

See current code below:

Text
Text
$StartDate = (Get-date).Addyears(-2) $EndDate = (Get-date) $src = "C:\Customer" $dst = "C:\TestingShareen\" Get-ChildItem -Path $src -Recurse | where-Object {($_.creationTime.Date -ge $StartDate.Date) -and ($_.creationTime.Date -le $EndDate.Date) -or ($_.LastWriteTime.Date -ge $StartDate.Date) -and ($_.LastWriteTime.Date -le $EndDate.Date)} | Copy-Item -Destination $dst -Recurse -Container -Verbose 

In the...

Powershell script for bulk upload which should send email on changes

$
0
0

Dear,

regarding Powershell I have two questions. I`m not a good expert in it.

I create a script which does update users based on a ID from a third party software.

Powershell
Import-ModuleActiveDirectory$excel=Import-Csv-PathC:\PS\output.csvforeach($userin$excel){Get-ADUser-Filter"employeeNumber -eq '$($user.userid)'"|Set-ADUser-givenName$($User.FIRSTNAME)-surname$($user.Lastname)-office$($User.LOCATION)-company$($User.CUSTOM03)}

So the update works fine.

1) Does the script like that only overrides the values if there is something different or does it always override? Because if there is anyway the value already in, it should not do anything

2) I would like that it sends out a mail after it did run with the changes which were made are in

Does someone have an idea on how to get it?

Thank you very much in advance

Sven

Powershell doesn't realise it started a process

$
0
0

So my script is basically just testing a couple of programs on the PC it's run on and even though it starts the processes as it should, it refuses to acknowledge this and always "hits if". Any ideas, fixes or better ways of doing this?

Powershell
functiontestApps{$arrApps,$arrShortApps=getArrApps$i=0foreach($crntItemin$arrApps){if(!(Start-Process$crntItem-ErrorActionSilentlyContinue)){$errorMessage="Software Test: $crntItem failed to launch"pauseScript$errorMessage}else{Write-Output"Software Test: $crntItem launched successfully"|Add-Content$outLogPath-ErrorActionSilentlyContinuesleep3Stop-Process$arrShortApps[$i]}$i++}}

Convert mailbox to shared/unlicense/add member(s) with one powershell script?

$
0
0

We often have to 'terminate' a user for client sites and this typically results in their mailbox being converted to a shared mailbox and the license needing to be removed from the mailbox. I always get an error when trying to convert to shared in office 365 admin center so I do the conversion in powershell then switch over to admin center to unlicense it and add members to the shared mailbox. I'd like to have my shared mailbox script do all of this(script below). My questions are;

  • How can I have the below script remove the license from the mailbox after converting it to a shared mailbox without me having to define a license type? We have multiple license types out there, varies from user to user. Surely there is a command to remove any/all license on a mailbox.
  • How can I have the script ask me who I'd like to add as a member of the...

Check for Error Confition in Powershell

$
0
0

Hi everyone.

I have a script that is working really well (thanks to a number of people in this community!) and before I put in a production environment I need to be able to check for an error condition. In this case it would need to check to see if a file currently exists in a particular directory.

The first line of the script renames a .tsv file. What would precede this line to check for the existence of the file? If the file does not exist i need the script to stop and then email the result. If the file exists then the rest of the script would run normally.

Powershell
get-childitem-Path\\myserver\tempprobedata\*.tsv|rename-item-NewName\\myserver\tempprobedata\import.txt# rename the .tsv file to .txtget-content\\myserver\tempprobedata\import.txt|select-Skip2|set-content"\\myserver\tempprobedata\tempimport.txt"# remove...

How to trim whitespace and keep table?

$
0
0

I am retrieving data from a database with a list of Employees that includes First name, last name, and employee code. The employee code on each employee has 4 spaces before the employee code( Ecode).

First Last EmployeeCode

.John.Doe. Ecode

.Sam .Smith . Ecode

I am able to remove the spaces with this:

Powershell
GetEmployees|ForEach-Object{$_.Employee_Code.trim()}

When I run that I only get a list of the employee codes and lose the other columns (First and Last name).

How do I remove the blank spaces and put the trimmed employee codes back into the table?

Thanks for the help

Find folders which share partially the same name without specifying the wildcard

$
0
0

Hi Everyone

I'm not sure if what I'm trying to achieve can be done. Maybe someone can help me.

I am trying to build a script which recognizes pattern matches of folder names and then moves all the matching folders into a new folder which is just the pattern mach.

For example, this is how my data could look like:

I have a folder in C:\Temp called "Sample Folders" which contains the following folders. each folder contains images. of course i have a lot more folders, that's why i want to build a script.

henry_highres_24072013
charlie_highres_25092015
richard_highres_01072014
henry_highres_10022016
richard_highres_11102017
charlie_highres_13032018
charlie_highres_16052012
levon_highres_10082011
henry_highres_15092018

now i know i can run e.g. a Get-Child-Item *Charlie* to get all the folders for Charlie. Want I want to achieve is to make PowerShell...

How to Pull Variables In Correctly

$
0
0

So I am trying to pull groups from AD. I have their correct names, however I am getting an error:

$Groups = "Group1", "Group2", "Group3"

Get-AdGroupMemeber - Identity $Groups -Recursive | Where objectclass -eq 'user'

I get the following:

What am I doing wrong? I did try to do a txt file and put the group in there but got the same message.

I had it "working" when I had $Group = "group1";"groups2" but I realized it was pulling the first group and that was it.

Any help will be great.

Combining Cmdlets/Variables

$
0
0

How would you guys coming the following variables and have ot go to the third variable

Powershell
#-- Get list of users in AD Groups --#ForEach($Groupin$Groups){$users=Get-ADGroupMember-Identity$Group-Recursive|Whereobjectclass-eq'user'}#-- Get list of users in AD specific OU --#ForEach($OUSin$OU){$usersfromou=Get-ADUser-SearchBase$OUS-Recursive|Whereobjectclass-eq'user'|Out-GridView}#-- From the AD Group Users, we are pulling certain information --#ForEach($userin$combined){$Inactive+=Get-ADUser-Identity$user-PropertiesLastLogonDate,Displayname,Enabled,EmailAddress|Where-Object{($_.LastLogonDate-lt$time)}|Select-Object-PropertyDistinguishedName,samAccountName,Name,Displayname,Enabled,EmailAddress,LastLogonDate

I want to be able to filter $users and $usersfromou and get it...

Testing if audio is playing

$
0
0

I'm trying to play an audio test on PCs that this is used on and even though I hear the audio, it says it failed in my script. I realise this is because my script is simply trying to see if an instance of WMP or VLCMP open up but obviously this is something different. So I'd like to know if perhaps there is a process that I can check for or if I could just simply do something like...

Powershell
if(!(playAudio)){Write-Host"D:"}else{Write-Host"Yay"}

The script as of now (again, I know VLC and WMP won't work but they're just placeholders for now).

Powershell
Add-Type-AssemblyNamepresentationCore$mediaPlayer=New-ObjectSystem.Windows.Media.MediaPlayerfunctiontestAudio{$arrMediaPlayers="VLC","WMP"playAudiosleep2if(!(Get-Process-Name$arrMediaPlayers-ErrorActionSilentlyContinue)){$errorMessage="Audio Test:...

Powershell - require input of parameter for desired output.

$
0
0

Hi all

I have very limited coding experience.

I would like to create a script that allows me into input certain parameters for output.

In this case I want to be able to enter a display name in order to return the group members. The code I have that works is below.

$o365Group = Get-MsolGroup -all | Where-Object {$_.DisplayName -eq “Onedrive_allowed”}

Get-MsolGroupMember -GroupObjectId $o365Group.ObjectId

Is there a way to insert an input for " DisplayName -eq “Onedrive_allowed” so I can run a generic script and be prompted to enter the group name?

Thanks

Darryl


How can you allow users to run only Custom PS Module Functions as Domain Admin

$
0
0

Morning everyone!

We have a help desk team that does not have domain admin privileges. 

I have created custom PS Functions as part of a custom Module that gets loaded into their PS when they open.

The issue is that they are running these functions under their own account which is limited.

Can anyone think of a way we could allow our help desk users to run all functions from ONLY this custom module in the context of Domain Admin?

Mike

Need help with using Powershell to develop a resume worthy project.

$
0
0

I am currently week on into Microsoft Software and Systems Academy. This course is cyber security based but our instructor has advised us to broaden our knowledge of IT. So I began researching and studying PowerShell. Where is a good place to start so I can fully understand PowerShell and its capabilities. Also my instructor is advising us to attempt a real life project on our own that we could post on our resume for experience (for example some students in a prior class created a website). So my mane question is what sort of project could I do with PowerShell? Thank you!

Need to Join 2 arrays fill into one until an object has 20 objects from 1 array

$
0
0

Here is the thing.

Array 1 has these objects: IPs, DC, PhysicalRegion

Array 2 has these objects: ServerName, DC, PhysicalRegion

The goal here is to have one array that will have IP, DC, PhysicalRegion, ServerName. Each Servername must have only 20 IPs

Here is the catch:

Each ServerName can get 20 IPs, but these IPs must be assigned 1st by DC to match the ServerName DC, then match PhysicalRegion. Once the ServerName has 20 IPs from the same DC, PhysicalRegion, then move to assign the next 20 IPs to the next ServerName.

Once there is no more ServerNames that match the IPs DC and PhysicalRegion, then match by only PhysicalRegion, until all the ServerName in the PhysicalRegion have 20 IPs


automatic Services

$
0
0

Hi Team,

I created script to check the service with set to automatic  by default and all running.

but i tried to stopped one of the service and verify.

and when i run my script it show " "All Auto Services are running!Yes"

and it doesn't display the servicename with stopped status.

see below my script if there is need to add:(

Text
$serviceauto=Get-WmiObject win32_service | 
Where-Object {$_.Startmode -eq "Auto"}|select Name,Startmode,State

if (!($serviceauto.state -eq "Running"))

{write-host "Please check $serviceauto.name"
}
else
{ write-host 

 "All Auto Services are running!Yes
}

Thanks

How to export result in csv

$
0
0
Text
$cluster=Get-WMIObject win32_service |Where-Object {$_.name -eq  "Clussvc"}|select Name,State,Startus
 foreach($service in $cluster)
 {
 $name=$service |where{$_.name -ne "$cluster"}
 if($true)
 {
  'Yes'|export-csv c:\temp\test.csv
 }
 
 else{
 'no'|export-csv c:\temp\test.csv
 
 }
 }

Hi Team,

Need your help here,

how can i export the condition Yes and no.

when i tried to run, its only blank?

please advise

Viewing all 15370 articles
Browse latest View live


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