piątek, 31 sierpnia 2012

Lately I meet such a problem, on which I was struggling for a week and couldn't find any solution.

Lady want to edit an excel file, to add some emails there and after clicking on a magical button those contacts should be imported to an Exchange distribution list.

My solution was to make a website written in PHP, that with use of might "shell_exec" will run Powershell script, which will:

1. Connect and read the *.xlsx file

2. remove all mail contacts that are mentioned in clicked distribution list,

3. Add mail contacts to exchange and associate them to appropriate distribution list.

4. Report the status on the operation

I was testing the script locally, I mean, I was simply running it from Powershell console, dealing with error handling and other stuff, the user interface has been left for the last stage of the project. As the script started to work properly the time has come and I have executed the query from PHP - just as I used to do.

The result was unexpected, because I have received.. zero, null, nothing, not even a single error! Just imagine how dramatic that moment has been! :)

So I have done what real detective should done - I was started to debug the script line by line (those main problematic lines are below), and what have appeared, was that line 3 - displaying my object - was correct, because from Powershell I was receiving:

 


And from PHP:




1. $ExcelDocumentPath = "C:\wamp\www\mail_contact_base.xlsx"

2. $ExcelDocument = New-Object -ComObject "Excel.Application"

3. $ExcelDocument <- here was still ok :)

4. $Workbook = $ExcelDocument.workbooks.open($ExcelDocumentPath) <- here was starting shit :)

5. $Workbook

6. $WorkSheets = $Workbook.Worksheets

Buuuut! The line no. 5 gave me...NULL! Not even something like "0" simply there was black page. So it has appeared that with no doubt I have problem with opening the excel object :/

I have redirected the line to 2>&1 to get something more, and what appeared was:

Exception calling "Open" with "1" argument(s): "Brak aktywnego okna widoku chro nionego." At C:\wamp\www\ps.ps1:4 char:42 + $Workbook = $ExcelDocument.workbooks.open <<<< ($ExcelDocumentPath) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation

It is in mine language, because I couldn't find zero of that error in English, it says about lack of "ProtectedViewWindow Object", my first thought was that I am in big shit...

I have posted that on http://wss.pl forum and one guru gave me link-> http://support.microsoft.com/default.aspx?scid=kb;EN-US;257757 saying that it would be as hard as I thought or harder :|

I have clicked from side to side trust options in office on that server, nothing helped. So I thought that I will reinstall MS Office application, the server was x64 and the Office was x32, so I thought this is the issue.

After reinstalling I have received another error - about wrong locale - from Powershell and from PHP:

 "Exception calling "Open" with "1" argument(s): "Old format or invalid type....blah blah stuff and other shit here"

But the languages were ok, so I was kind of missing my old error, and I have reinstalled MS Office once again for the same as it was - x32.

After that operation - reinstalling MS Office - the script was running perfectly from Powershell - just as it was before, but in PHP I was receiving..the lack of privileges - no access to the file- deeeeeep shiiit.

I was near becoming insane, it was the last day for finishing that project in, let's say, first work version, and I had nothing. So I started to ask uncle Google, what to do, I still wanted to have my old "lack of ProtectedViewWindow Object" error.

Surprisingly I have found this:

http://stackoverflow.com/questions/5516221/excel-application-not-working-in-Powershell-using-sql-job-but-works-from-comman

The guy is saying that I should do this:

    1. Add Directory: C:\Temp
    2. Add Directory: C:\Windows\SysWOW64\config\systemprofile\Desktop

That should fix the problem.


Indeed there was no “temp” directory on a folder neither “desktop”. I have added them, and run the script, and I have received.. NULL. But wait a minute! I have redirected the errors to standard output, so if there is no error on the page.. is it possible!?!?!

Yup, the guy saved my life :) there was no error about "ProtectedViewWindow" more, and the mail contact from Excel file, has showed up in the Exchange and later on the appropriate distribution list J

środa, 22 sierpnia 2012

SCCM – problem during advertise a package to a client, just tmp files.

SCCM, for me personally, will forever remain a bottomless well of endless problems :)
Lately I meet a problem that the package has been distributed to all Distribution Points correctly, but when it came to deliver it to clients, in local cache folder I saw only *.tmp files, all folders were correct, just there was no normal files.
If somebody has met problem like this here is the solution, at least for mine case:

There is a file: C:\Windows\System32\inetsrv\config\applicationHost.config on SCCM server
and you can find there many record like:
<add fileExtension=”.config” allowed=”false” />
Now it is easy to figure out, that the install folder of application I wanted to deploy in cache, contained one file with “.config” extension. As I changed “false” to “true” in that record, all worked like a harm.

SCCM – Different data in Resource Explorer and Add/Remore Programs while running report

Recently my colleague tried to make a report with workstations that have particular version of software – Adobe Reader. But he meet a problem because in report with Adobe Reader 9, he had workstations that definatelly had version 10, the same with version 8 – there was completely mess, as he was checking the Add/Remove Programs on the workstation it appeared it has some other version.

I found it indeed very weird,and tried to solve. By making directly queries on the database I noticed that indeed v_Add_Remove_Programs (name in SCCM report ->SMS_G_System_Add_Remove_Programs) shows old stuff – 9 version of Adobe Reader, but the database view v_SoftwareProducts (name in SCCM report -> SMS_G_System_SoftwareProducts) shows correct data – version there was 10.

That was even double weird for me :) but after some time with google I have finally found the explanation.
The “Add/Remove Programs” stuff in updated by… “Hardware Inventory” client task, while “Software Products” as you can figure it out from it’s name – by “Software Inventory task”.

My hardware inventory period was set to one week, while software for 1 day. Lost for this almoust two days…

wtorek, 21 sierpnia 2012

piątek, 17 sierpnia 2012

Powershell - how to replace word/character in a string.

Recently I was looking for a good way for replacing words in a string, and found this very helpful page with many methods, hope you find it useful :

http://powershell.com/cs/blogs/tobias/archive/2011/04/28/multiple-text-replacement-challenge.aspx