Friday, August 13, 2010

PowerShell, double and single quotes

Powershell and quotes can be a bit confusing sometimes.

Double-quoted strings are subject to variable substitution, while single-quoted strings are not.

For example, let's launch a PowerShell console:

C:\Users\User>$x="hello"
C:\Users\User>"$x"
hello
C:\Users\User>'$x'
$x
C:\Users\User>
Let's write now a very small script that outputs a single parameter:

C:\Users\User>'param($p) "$p"' > .\test.ps1
C:\Users\User>type .\test.ps1
param($p) "$p"

Here's the output when calling that script with double- and single-quoted strings:

C:\Users\User>$x="hello"
C:\Users\User>.\test.ps1 "$x"
hello
C:\Users\User>.\test.ps1 '$x'
$x
C:\Users\User>.\test.ps1 -p "hello world"
hello world
C:\Users\User>.\test.ps1 -p 'hello world'
hello world
Which makes sense, because in PowerShell both single and double quotes serve to form strings with spaces.

Now, let's open a normal command prompt and try a few things:

C:\Users\User>powershell -file test.ps1 -p "hello"
hello
C:\Users\User>powershell -file test.ps1 -p 'hello'
'hello'
C:\Users\User>set XYZ="hello world"
C:\Users\User>powershell -file .\test.ps1 %XYZ%
hello world
C:\Users\User>set XYZ='hello world'
C:\Users\User>powershell -f .\test.ps1 %XYZ%
'hello
Which is expected too, because in a DOS command prompt, only double quotes will form strings with spaces, and single-quotes are taken as literals.

Tuesday, March 9, 2010

EDM designer in Visual Studio: "The operation could not be completed. The parameter is incorrect"

When trying to open a .edmx file in VS, it throws this error:



Solution: open the designer as plain xml and remove children of edmx:Diagrams/Diagram node.

EDM: Error 11011: Association End key property 'Id' is not mapped.

Problem: receiving this error when building a project that includes an EDM:
Error 11011: Association End key property 'Id' is not mapped.
Solution: open as plain xml the .edmx file, remove everything related to the entity causing the error, then open the model with the designer and "Update model from database..." After you build, you should not get this error anymore.

Thursday, March 4, 2010

MVC & ViewState don't work together

It seems that postbacks are not supported in MVC. I found this article, and tried to address this issue in order to use some Ajax controls that do require form tags for including a ScriptManager, which cause postbacks.

In short - it worked correctly, with one tweak: instead of
public class ViewUserControlWithoutViewState<T> : ViewUserControl<T> where T : class 
I changed to:
public class ViewUserControlWithoutViewState : ViewUserControl
Otherwise, ASP.Net was not finding the type. Why? Didn't dig into more, but it may be related to Reflection not resolving correctly when where keyword is being used.

BTW - this is the control I was trying to use (needs Ajax Controls Toolkit):
    <form id="Form1" runat="server">
<asp:ToolkitScriptManager ID="asm" runat="server" />
<asp:LinkButton ID="linkButton1" runat="server" Text="Show Popup" />
<asp:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" TargetControlID="linkButton1" ConfirmText="Want it or not?" />
</form>

Tuesday, January 12, 2010

Reindex script for SQL Server

DECLARE @db_name nvarchar(1000)
SET @db_name=N'your database name'
EXEC ('USE ' + @db_name)
DECLARE @index_name nvarchar(1000)
DECLARE @table_name nvarchar(1000)
DECLARE @ind_id int
DECLARE index_cursor CURSOR for
select name, object_name(id), indid from sysindexes where id > 1000 and indid >=1 and rows > 0 order by object_name(id), indid, name
OPEN index_cursor
FETCH NEXT FROM index_cursor INTO @index_name, @table_name, @ind_id
WHILE @@FETCH_STATUS = 0
BEGIN
IF @ind_id = 1
BEGIN
EXEC sp_executesql N'DBCC DBREINDEX (@tbl, @idx, 0)',
N'@idx nvarchar(1000), @tbl nvarchar(1000)',
@index_name,
@table_name
END
EXEC sp_executesql N'DBCC INDEXDEFRAG (@db, @tbl, @idx)',
N'@db nvarchar(1000), @idx nvarchar(1000), @tbl nvarchar(1000)',
@db_name,
@index_name,
@table_name
FETCH NEXT FROM index_cursor INTO @index_name, @table_name, @ind_id
END
CLOSE index_cursor
DEALLOCATE index_cursor

Thursday, December 24, 2009

How to create a bootable installation CD/DVD in Windows

1) Download BBIE
2) extract bootable sector from a bootable disk
3) Download ImgBurn
4) Create a bootable image file

Run ImgBurn
Choose "Create image file from file/folders"
Include desired files and folders
Click "Advanced", "Bootable Disc" (sic), specify as "Boot Image" file the file you obtained in step 2).

Launch the process, it will result in a .img file.

5) Create CD/DVD

Relaunch ImgBurn
Choose "Write image file to disc"
Browse to the image file obtained in step 4).

If everything goes well, you should end up with a bootable CD/DVD.

Thursday, August 13, 2009

How to create a PFX file

makecert.exe -sv MyKey.pvk -n "CN=My Test Site" MyKey.cer -sky exchange

"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\pvk2pfx.exe" -pvk MyKey.pvk -spc MyKey.cer -pfx MyPFX.pfx -po

Note: "-sky exchange" avoids errors like "SSL Certificate add failed, Error: 1312"